我具有对象数组的角度分量
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();
}
}
这可以解决问题,但是还有其他解决方案吗?
问题在于您的构造函数。
constructor(private iterableDiffers: IterableDiffers){
this.iterableDiffer = iterableDiffers.find(this.config).create()
}
正在使用Stackblitz:-https://stackblitz.com/edit/angular-uqivci