2017-02-04 60 views
1

我正在尝试在我的Angular应用程序上构建一个组件,并得到一些问题。让我来解释......在Angular APP上更新ngFor

我有这个ngFor:

<tr *ngFor="let processo of processos; let i = index; trackBy: trackById;"> 
我component.ts

我有这样的:

processos: Array<any> 

当我尝试添加一个新的过程中,我收到一个新的文档,然后存储在我的LocalStorage上:

  this.advogadoService.addProcess(data) 
       .subscribe(
        process => { 
         this.updateService.update(process, 'processos'); 
         this.processos = JSON.parse(localStorage.getItem('processos')); 
         this.update(process._id); 
         resolve(process); 
        }, 
        err => reject(err.json().message) 
       ) 

到目前为止,一切正常。我收到了数据,并更新了我的观点。 现在,我的问题开始......

我有这样的代码:

let updateData = Observable.interval(30000) 
     .take(60) 
     .flatMap(() => { 
      return this.advogadoService.getOne(id) 
     }) 

    this.search = updateData.subscribe(
     doc => { 
      if(doc.processType) { 
       this.updateService.update(doc, 'processos'); 
       this.processos = JSON.parse(localStorage.getItem('processos')); 
       this.stopNow(); 

       iziToast.success({ 
        title: 'Processo atualizado', 
        message: `O processo ${doc.processNumber}, foi atualizado com sucesso!`, 
        icon: 'fa fa-bell-o', 
        layout: 2, 
        balloon: true, 
        timeout: 10000, 
       }) 
      } 
     } 
    ) 

} 

stopNow() { 
    this.search.unsubscribe() 

} 

此代码尝试60次,在30秒的时间间隔获得更新的数据之前发送。当我收到新的数据,把他放在LocalStore,以及与此命令更新我的看法:

this.processos = JSON.parse(localStorage.getItem('processos')); 

,但我认为不能以新的数据刷新,直到我点击一些对象上对我的看法,就像按钮上的例子。

我每次收到新数据时都需要更新我的视图。

我最好的办法可以做到这一点?

感谢

回答

0

采用了棱角分明的ngOnChanges是为这种情况的一个很好的解决方案,因为它是一个生命周期挂钩时调用的指令改变的任何数据绑定属性。更多的信息在这里:https://angular.io/docs/ts/latest/api/core/index/OnChanges-class.html

+0

已经尝试这...不工作在我的情况下......也许我做错了方式..你可以发送一个例子吗?谢谢。 –