angular2组件刷新数据

时间:2016-09-12 12:40:10

标签: angularjs angular components

我的anuglar2组件中有一段代码,它在组件初始化阶段加载数据 - 我希望在页面刷新期间更新数据,我做错了什么?

这是我的片段:

   ngOnInit(): void {
        this._productService.getProducts().subscribe(
            products => this.products = products,
            error => this.errorMessage = <any>error);
    }

1 个答案:

答案 0 :(得分:0)

我认为问题在于 ngOnInit()。只有在初始化组件时才会调用它一次。刷新时, ngOnInit()将不会再次调用。因此,您应该在构造函数或其他自定义函数中加载数据。< / p>

constructor(private _productService:productService){
   _productService.getProducts().subscribe(
                                 products => this.products = products,
                                 error => this.errorMessage = <any>error);
}

// make sure you call it from somewhere.
GetProducts()
{ 
      this._productService.getProducts().subscribe(
            products => this.products = products,
            error => this.errorMessage = <any>error);
}