Network.onConnect(...) doesn't trigger view update

I’m trying to make an “upload” button in my app hide or show based on the device’s network connectivity. I have the following code:

let deviceConnectedToInternet = this.network.onConnect().subscribe(ret => {
    this.deviceConnected = true;
});
let devicedisconnectedFromInternet = this.network.onDisconnect().subscribe(ret => {
    this.deviceConnected = false;
});

If I put a console.log in the arrow function, I can see that the observable is correctly triggering when the network connects or disconnects, and my boolean value is changing. But the view isn’t updating to reflect that change.

Any help is appreciated.

Try to use zone

constructor(private zone: NgZone) {
}

yourMethod() {
    let deviceConnectedToInternet = this.network.onConnect().subscribe(ret => {
      this.zone.run(() => {
         this.deviceConnected = true;
      });
   });
}

I did read about that a few different places, but all of the examples were a bit too complex to understand. Yours is perfectly simple, thank you!

1 Like

No worries, let me know if it solves the problem :crossed_fingers:

That did the trick. Thank you so much!

1 Like

Coolio thx for the feedback