Angular 2 doesn't update * sometimes *

I have an Angular 4 app running on Electron.

In my main component, I start a service that opens the saved file and sets its parameters for an object named "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 the session file or load the last session file when I initialize the application. I am updating my interface by subscribing to a "ready" event that my service emits when it has finished downloading a file. Note that it does the same method to open the most recent file and download the file using a dialog.

Problem: When opening a file through a dialog, the user interface is immediately updated. Loading the last session on startup does not refresh the user interface.

What I have tried The service uses NgZone to trigger change detection. The subscription shows the correct session is set to the variable 'this.session' I tried to use ChangeDetectorRef but had no difference.

Why is there a difference in change detection when I use the same method and event to fire it? Does this have to do with components that are still being initialized?

Refresh to show the zone update in the 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 a "Ready" event

+3


source to share


1 answer


Why not use it LoggerService

like this:

this.logger.tick_then(() => this.session = this.Service.session);

      



This will change the next change detection cycle

0


source







All Articles