I am using 'ngFor' directive to initialize a list of child components:
(我正在使用ngFor指令初始化子组件列表:)
Parent component:
(父组件:)
<div *ngFor="let item of serviceGetter.items | async; let i = index">
<child-component [item]="item" [currentIndex]="i"></child-component>
</div>
Service:
(服务:)
@Injectable()
export class TermsService {
private dataStore: {
data: any[];
} = { data: [] };
get items() {
return this._data$.asObservable();
}
//data are populated inside this method
someMethod() {
this.dataStore.data= result;
this._data$.next(Object.assign({}, this.dataStore).data);
}
updateSpecificItem() {
....
this.dataStore.data[index] = {
...somedata,
};
}
}
'Child component' change detection is set to 'OnPush'.
(“子组件”更改检测设置为“ OnPush”。)
The problem is when 'updateSpecificItem()' function is called child component at 'index' position is reinitialized(constructor call). (问题是当“ updateSpecificItem()”函数在“索引”位置的子组件被重新初始化(构造函数调用)时。)
Is there a solution to avoid this or is this the accepted behavior? (有解决方案可以避免这种情况吗?还是这是公认的行为?)
ask by tsiro translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…