如何在 Angular 中自动刷新页面 [英] How to auto refresh page in Angular

查看:169
本文介绍了如何在 Angular 中自动刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的页面打开,我想自动重新加载我的页面(自动回发以重新加载表数据).为此,我编写了一些代码,但这不起作用.在下面查看我的代码

I want to reload my page automatically(auto postback to reload table data) if my page is open. For that I have written some code but that is not working. See my code below

import { Component, OnInit } from '@angular/core';
import { Observable,Subscription, interval  } from 'rxjs';

export class ProductsComponent implements OnInit {
  private updateSubscription: Subscription;

  ngOnInit() {
     this.updateSubscription = interval(3000).subscribe(
     (val) => { this.getProducts()});

    });
  }

  private getProducts()
  {
    this.employeeService.getProducts().subscribe((data:any) => {
      console.log(data);
     this.products=data;});

  }

}

getProducts 调用 rest Api.我怎样才能让它工作?

getProducts calling rest Api. How can I got it working?

推荐答案

给定的代码对我有用.最初我正在等待页面重新加载.但是当我将调试点放在被检查元素的每个间隔中时,我的代码正在执行.

Given code in question working for me. Initially I was waiting page to reload. But when I put debug point in inspected element every interval my code was executing.

import { Observable,Subscription, interval  } from 'rxjs';
export class ProductsComponent implements OnInit {
  private updateSubscription: Subscription;

  ngOnInit() {
     this.updateSubscription = interval(3000).subscribe(
     (val) => { this.getProducts()});

    });
  }
}

这篇关于如何在 Angular 中自动刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆