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


#1

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.


#2

Try to use zone

constructor(private zone: NgZone) {
}

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

#3

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!


#4

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


#5

That did the trick. Thank you so much!


#6

Coolio thx for the feedback