【发布时间】:2020-09-15 15:08:56
【问题描述】:
我有带有对象数组的角度分量
export class AlertsConfigListComponent implements OnInit, DoCheck {
@Input() config: ProductAlertConfig[];
并使用 IterableDiffer 获取此数组的更改:
constructor(private iterableDiffers: IterableDiffers) {
this.iterableDiffer = iterableDiffers.find([]).create(null);
}
ngDoCheck(): void {
let changes : IterableChanges<ProductAlertConfig> = this.iterableDiffer.diff(this.config);
if (changes) {
this.doSmth();
}
}
它有效,每次更改数组时我都可以更改。 所以现在我的问题是。如何在 changes 对象中检查数组大小已更改,因为它在我对该数组进行排序时也会触发。 IterableChanges 对象中没有用于此目的的属性。
如果我能得到新大小的数组,我会这样做:
ngDoCheck(): void {
let changes : IterableChanges<ProductAlertConfig> = this.iterableDiffer.diff(this.config);
if (changes && newSize !== oldSize) {
this.doSmth();
}
}
它会解决问题,但还有其他解决方案吗?
【问题讨论】:
-
不,我不认为这是我需要的,因为示例中的问题是他们将获取数组中的每个值并检查属性更改。但我需要检查何时从该数组中添加或删除元素
-
试试这个:- this.iterableDiffer = iterableDiffers.find(this.config).create();
-
我在下面添加了我的答案。和一个有效的堆栈闪电战。
标签: javascript angular iterable angular2-changedetection