Reputation: 147
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);
}
}
Upvotes: 3
Views: 2190
Reputation: 83
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.Upvotes: 1
Reputation: 391
I don't think it will as EventEmitter is an Observable, thus I don't think it is wrapped in NgZone.
Although don't rely on Change Detection defaults if possible. Just use OnPush.
Upvotes: 0