inside of child component, use settimeout in ngOnInit to mutable change person.
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);
}
}
setTimeout() is run, there is a change detection cycle at the top of the tree (a 'tick', I guess). This is still true when using OnPush. However, in this situation, this does not necessarily mean change detection will reach the view of the component which setTimeout() was called from.EventEmitter has a handler/listener registered, calling emit will result in the views being marked dirty up to the root (markForCheck()-style).setTimeout(), and the parent component has registered a handler for that signal, you will have a change detection cycle for your component after the function is run.setTimeout() in a function passed to this.zone.runOutsideAngular() to bypass the change detection cycle.ngDoCheck() on that child component. It is run for each change detection cycle of the (parent) component.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With