如果已进行更改,则检测Observable <any []>

时间:2018-01-19 06:04:57

标签: angular observable

如果已经从列表中更改了(添加,删除,更新),有没有办法检测/观察/观看/订阅Observable<any[]>

_objList: Observable<any[]>;


onDelete(i: index): void {
    this._objList.subscribe(list => {
        // delete from list logic...
    });
}

onAdd(data: any): void {
    this._objList.subscribe(list => {
        // add to list logic...
    });
}

OnListChanged(): void {
     // Do something if changes has been made to _objList.
}

1 个答案:

答案 0 :(得分:1)

在RxJS中使用BehaviorSubject。然后,您可以订阅或发布更改

_obList = new BehaviorSubject<any[]>([]);

此处的代码示例:https://stackblitz.com/edit/angular-zabmwe,请参阅app/app.component.ts文件。

有关BehaviorSubject的更多说明:https://scotch.io/tutorials/3-ways-to-pass-async-data-to-angular-2-child-components#toc-solution-3-use-rxjs-behaviorsubject