I have a Angular 4 Application running on Electron.
In my main component I run a service which opens a saved file and sets its parameters on an object called 'session'.
ngOnInit(): void {
this.Service.ready.subscribe(() => {
this.session = this.Service.session;
console.log(this.session.name)
});
this.Service.startService();
}
I either use the file dialog to open a session file or I load the last session file when initializing the application. I update my UI by subscribing to the 'ready' event that my service emits when it has finished loading the file. Note that for both opening the most recent file and loading a file using a dialog, it runs the same method.
The problem: When opening a file a file through the dialog, the UI get's updated immediately. When loading the last session on startup, the UI doesn't get updated.
What have I tried The service uses NgZone to trigger change detection. The subscription shows that the correct session is set to the variable 'this.session' I have tried using ChangeDetectorRef but no difference.
Why is there a difference in change detection when I use the same method and event to trigger it? Does it have to do with components that are still being initialized?
Update to show Zone updating in Service
from my Service:
loadSession(path: string): void {
fs.readFile(path, { encoding: 'utf-8' }, (err, data) => {
if (!err) {
var session = JSON.parse(data);
console.log(session);
this.zone.run(() => {
this.session = session;
this.readyToStart();
this.notify("Opened Session:", this.session.name, null);
console.log("opened session");
});
} else {
console.log(err);
}
});
}
My readyToStart method emits the 'Ready' Event