How do you initiate change detection on a child component from the parent if the change detection is set to OnPush? The array of objects are maintained in the parent component and for my case the change to the child value (item in the array) is initiated from the parent.
app.component.html:
<ul>
<li *ngFor="let item of items">
<button mat-button (click)="onClickMe(item)">Basic</button>
<hello [item]=item></hello>
</li>
</ul>
app.component.ts (relevant part):
export class AppComponent {
items: Item[] = [];
constructor() {
let i: number = 0;
for (i=0; i<10; i++) {
let tmps: ItemTmp[] = [];
for (let j=0; j<10; j++) {
tmps.push({
value: ("tmp_" + j),
selected: true
});
}
this.items.push({
id: i,
name: ("item_" + i),
tmps: tmps
});
}
}
onClickMe(item: Item): void {
item.tmps[0].selected = !item.tmps[0].selected;
console.log(item.tmps[0].selected);
}
}
hello.component.ts:
@Component({
selector: 'hello',
template: `
<h1>{{item.id}} - {{item.name}}</h1>
<li *ngFor="let tmp of item.tmps">
{{tmp.value}} - {{tmp.selected}}
</li>
`,
styles: [`h1 { font-family: Lato; }`],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class HelloComponent {
@Input() item: Item;
}
I have created an example project showing my question.
https://stackblitz.com/edit/angular-ivy-7v5pef?file=src/app/app.component.html
In this example, I need for the value in the child object to update on the screen. I can see in the console that the object in the array in the parent is updating as I would expect. But the child component is not changing.