- parent component has one child component(child1)
- child1 component has an input property person and OnPush changeDetection strategy
inside of child component, use settimeout in ngOnInit to mutable change person.
- normally dom view will not update because of the onPush strategy
- however if I use event emitter to emit this mutable change to its parent which in turn change the property binding(mutable), the view gets update.
So my question is here, will event emitter trigger view check?(change detection). Thanks!
parent component
@Component({
selector: 'app-root',
template: '<app-child1 [person]="person", (changOnPerson)="onPersonChange($event)">',
})
export class AppComponent {
person: Person = {
name: 'Alex',
age: 20
};
constructor() {
}
onPersonChange($event) {
this.person = $event.change;
}
}
child1.compnent
@Component({
selector: 'app-child1',
template: '{{person | json}}',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class Child1Component implements onInit {
@Input() person: Person;
@Output() changOnPerson = new EventEmitter();
constructor() { }
ngOnInit() {
setTimeout(() => {
this.person.name += ' ,abc';
// core code here !
this.changOnPerson.emit({ change: this.person });
}, 2000);
}
}