I'm learning Angular's change detection and can't find an answer why the following snippet doesn't work.
Suppose we have a Component
with ChangeDetectionStrategy.OnPush
set:
@Component({
selector: 'material-app',
templateUrl: 'app.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent {
public isBusy: boolean;
public message: any;
public version = VERSION;
constructor(
private readonly httpClient: HttpClient,
private readonly dialog: MatDialog) {
}
public onSeeBugClick(): void {
this.sendApiRequest();
this.dialog.open(DialogComponent, {
width: '250px'
});
}
private sendApiRequest(): void {
this.message = 'Sending request';
this.isBusy = true;
this.httpClient.get<void>('https://jsonplaceholder.typicode.com').pipe(
finalize(() => {
this.isBusy = false;
}))
.subscribe(
(): void => {
console.log('Success from jsonplaceholder.typicode.com');
this.message = 'Success';
},
(error: string): void => {
console.log('Error from jsonplaceholder.typicode.com');
this.message = 'Error '+ error.toString() + ': ' + JSON.stringify(error);
});
}
}
Here is StackBlitz.
When API request ends up with an error, neither ENABLE ME BACK
button disabled state gets updated, nor message text below it do. But if I comment ChangeDetectionStrategy.OnPush
line - it works fine.
Could anyone explain why ChangeDetectionStrategy.OnPush
strategy doesn't work in this case?