简体   繁体   中英

How to use ngFor trackBy but notify about component input change?

 <div *ngFor="let obj of objs; trackby:objToId"> <sample-component [obj]="obj"></sample-component> </div> 

Actual behaiviour :

  • sample-component is not notifed about changes in obj attributes.

Desired behaiviour :

  • sample-component should be notifed about any obj attribute change without rerendering component.

How to achieve desired behaiviour ?

If you're mutating some property of obj , Angular's change detection won't notice that (doing shallow comparison), trackBy is not guilty here.

Try overwriting the entire obj , so the object reference changes. So, for example, instead of

objs[n].someProperty = newValue;

do

objs[n] = Object.assign({}, objs[n], { someProperty: newValue });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM