I have two components, where I use ChangeDetectionStrategy.OnPush. A parent component:
@Component({
changeStaregy: ChangeDetectionStrategy.OnPush,
template:`
<button (click)="onClick()">clear</button>
<div>
<test [model]="model"></test>
</div>`
})
export class AppComponent {
model: TestModel;
constructor(){
this.model = { id: 1, text: 'bla bla bla'}
}
onClick() {
this.model = new TestModel();
}
}
and a child component that just displays a data:
@Component({
changeStrategy: ChangeDetectionStrategy.OnPush,
selector: 'test',
template: `
<div>
<div> {{model.id}} </div>
<div> {{model.text}} </div>
</div>`
})
export class TestComponent {
@Input() model: TestModel;
}
When I click on the button "clear", it calls onClick() function, which assigns an empty entity to "model". This triggers a change detection, because the input was changed (OnPush strategy). But if I wrap assignment with an async call, the change detection doesn't work and therefore UI is not updated:
onClick() {
setTimeout(() => {
this.model = new TestModel();
}, 2000);
}
Angular2+ has NgZone which patches a setTimeout function. The patched setTimeout must trigger the change detection, but in my case it doesn't. Why the change detection doen't work? How can I fix it?