The change detection works in the following scenarios where the data changes needs to update the application HTML.
ApplicationRef.tick()
@Component({ selector: 'app-event-listener', template: ` <button (click)="onClick()">Click</button> {{message}}` }) export class EventListenerComponent { message = ''; onClick() { this.message = 'data updated'; } }
data = 'default value'; constructor(private httpClient: HttpClient) {} ngOnInit() { this.httpClient.get(this.serverUrl).subscribe(response => { this.data = response.data; // change detection will happen automatically }); }
data = 'default value'; ngOnInit() { setTimeout(() => { this.data = 'data updated'; // Change detection will happen automatically }); }
data = 'initial value'; ngOnInit() { Promise.resolve(1).then(v => { this.data = v; // Change detection will happen automatically }); }