Angular2监视对象/数组更改(Angular2 final> = 2.1.1)

时间:2016-11-24 10:38:23

标签: angular typescript

我想观看一个对象/数组,可以通过服务或控制器例程进行编辑。我以为Observable可以观看一个对象/数组。

我的实施不会对项目的更改做出反应:

  private data : Observable<Array<any>>;
  private dataObserver: Observer<Array<any>>;
  private sub : Subscription;
  private items: <Array<any>>;

  ngOnInit() {
     this.items = itemService.getItems();
     this.data = new Observable<Array<any>>(observer =>{
        this.dataObserver = observer;
     });
     this.data.subscribe(
        x => console.log('onNext: %s', x),
        e => console.log('onError: %s', e),
        () => console.log('onCompleted')
     );
     this.dataObserver.next(this.items);
  }


private start(){

  //change values of the array in an interval
  let loop = Observable.interval(250)
  let i=0;
  self.sub = loop.subscribe(() => {
      if(self.items[0]){
        self.items[0].id= i;
        if(i<100) i++;
        else i=1;
      }
  })
}

观察次数subciption对items数组的更改没有反应。它只会在下一个mehtod上触发。另一方面......对于简单的手表方法来说,这太麻烦了。

angular-2为我们提供了什么样的变化,就像$ scope一样。$ watch在angular-1中做了什么?

3 个答案:

答案 0 :(得分:13)

Angular2提供IterableDiffer(数组)和KeyValueDiffer(对象)来获取有关两次检查之间差异的信息。

NgClass是一个很好的例子https://github.com/angular/angular/blob/14ee75924b6ae770115f7f260d720efa8bfb576a/modules/%40angular/common/src/directives/ng_class.ts#L122

另见https://angular.io/docs/ts/latest/api/#!?query=differ

一个例子

// inject a differ implementation 
constructor(differs: KeyValueDiffers) {
  // store the initial value to compare with
  this.differ = differs.find({}).create(null);
}

@Input() data: any;

ngDoCheck() {
  var changes = this.differ.diff(this.data); // check for changes
  if (changes && this.initialized) {
    // do something if changes were found
  }
}

答案 1 :(得分:4)

在导入部分:

import { DoCheck, KeyValueDiffers, KeyValueChangeRecord } from '@angular/core';

添加'KeyValueDiffers'注射器:

private _differ: any;    

constructor(private _differs: KeyValueDiffers) {
    this._differ = _differs.find({}).create();
}

最后跟踪变化:

ngDoCheck() {
    const change = this._differ.diff(this.Your_Object_To_Track);
    if (change) {
        change.forEachChangedItem(
            (record: KeyValueChangeRecord<any, any>) => {
                console.log(record.key + ': ' +  record.previousValue + '=>' + record.currentValue) });

        change.forEachRemovedItem(                
                (record: KeyValueChangeRecord<any, any>) => {
                    console.log(record.key + ': ' +  record.previousValue + '=>' + record.currentValue) });

        change.forEachAddedItem((record: KeyValueChangeRecord<any, any>) => {
                console.log(record.key + ': ' +  record.previousValue + '=>' + record.currentValue) });
    }
}

答案 2 :(得分:1)

感谢您的示例Gunter。

我根据您的回复选择快速解决方案。

我通过DoCheck实现替换OnInit句柄。现在,如果我的服务外部名为Language的值发生更改,则视图会更新。

在我的案例中:

import { Component, DoCheck } from "@angular/core";
export class LangListUserComponent implements DoCheck {

  constructor(private _languageService: LanguageService)
  {}

  ngDoCheck() {
    /** Get available lang */
    this.oLanguages = this._languageService.getLanguageList();
    this.setCurrentLang(this.oLanguages);
  };
}