Inside my class I have a property isOffline to keep track of the connection status.
I initialize this property and inside my component’s constructor I have 2 event listeners for ‘online’ and ‘offline’. Both listeners update the this.isOffline property.
Inside my page’s HTML I display the property via [(ngModel)].
When the page is loaded the initial connection status is displayed. The even listeners should work properly, as the updated status can be displayed with console.log inside the listeners. However, my UI does not update the status.
I have also tried to put the isOffline inside an object (so this.connection.isOffline instead of this.isOffline), but when I try to update the isOffline property I get an error stating I cannot change read only properties.
When you update component values inside functions form other libraries, Angular 2 doesn’t trigger the change, so you may need to correctly use arrow functions to avoid that, or use NgZone as last resource:
With the $scope.$apply in place I can get the current value of the property.
What is this in Angular 2?
If I am correct EventEmitters are only for communication between multiple components. Yesterday I have tried to put it inside a run() with NgZone, but it did not seem to work.
I was having a similar issue with address variable not getting updated every time on changing google maps marker. But, with NgZone, that issue is resolved once and for all.