I've noticed a strange behavior of change detection in Angular. When Observable updated as in the example, change detection not triggered for some reason.
The key here is setTimeout
called inside the callback, if you remove it, change detection will work fine. markForCheck
which inside AsyncPipe
also called as it should.
@Component({
selector: 'my-app',
template:
'<button (click)="click()">Trigger</button> <br> {{value$ | async}}',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
readonly value$ = new BehaviorSubject(1);
constructor(
private readonly zone: NgZone,
) {}
click() {
this.zone.runOutsideAngular(() => {
setTimeout(() => {
this.value$.next(this.value$.value + 1);
console.log(`Change (Should be ${this.value$.value})`);
});
});
}
}