[使用IterableDiffer获取Angular 8中的数组大小更改

问题描述 投票:1回答:1

我具有对象数组的角度分量

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();
}

}

这可以解决问题,但是还有其他解决方案吗?

javascript angular iterable angular2-changedetection
1个回答
0
投票

问题在于您的构造函数。

constructor(private iterableDiffers: IterableDiffers){
   this.iterableDiffer = iterableDiffers.find(this.config).create()
}

正在使用Stackblitz:-https://stackblitz.com/edit/angular-uqivci

© www.soinside.com 2019 - 2024. All rights reserved.