I would like to udpate my application view, triggered by events from service.
One of my services injects the ChangeDetectorRef. Compilation works, but I am getting a
To use ApplicationRef is a good solution. I have a few more solutions:
Using setTimeout
@Injectable()
export class MyService {
private count: number = 0;
constructor(){}
increment() {
setTimeout(() => this.count++, 0);
}
}
Using Promise
@Injectable()
export class MyService {
private count: number = 0;
constructor(){}
increment() {
Promise.resolve().then(() => this.count++);
}
}
Both solutions force Angular to trigger change detection, if you are using Zone.js for change detection - this is the default way and 99% of the apps use it.