# Update ui based on network status

**URL:** <https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350>\
**Category:** ionic-v3\
**Created:** [June 14, 2017, 6:14pm UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350 "2017-06-14T18:14:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashley\_shookhye](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ashley\_shookhye](https://forum.ionicframework.com/u/ashley_shookhye)\
**Post date:** [June 14, 2017, 6:14pm UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/1 "2017-06-14T18:14:54Z")

</div>

Hello, I want to be able to update an ionic page based on the network status. the function is being called but the page is not being updated. Grateful if some advice can be given.

Message Service that sends the messages to all subscribers.

```auto
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Subject } from 'rxjs/Subject';

/*
  Generated class for the MessageProvider provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class MessageProvider {

  private subject = new Subject<any>();
  constructor() {}

  sendMessage(message: string) {
    this.subject.next(message);
  }

  clearMessage() {
    this.subject.next();
  }

  getMessage(): Observable<any> {
    return this.subject.asObservable();
  }
}

```

The code to watch the network status is as follows and are found in app.component

```auto
network.onDisconnect().subscribe(() => {
        console.log(0);
        MessageProvider.sendMessage('Not Connected!');
      });

network.onConnect().subscribe(() => {
        console.log(1);
        MessageProvider.sendMessage('Connected!');
      });

```

The message is being transmitted to the page.ts but the UI is not getting updated.

```auto
message: any;
subscription: Subscription;
    ionViewDidLoad() {
        this.subscription = this.MessageProvider.getMessage().subscribe(message => {
          console.log(message); 
          this.message = message; 
        });
      }

```

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [June 14, 2017, 8:29pm UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/2 "2017-06-14T20:29:46Z")

</div>

I find it supremely confusing when people don’t follow ordinary JavaScript naming conventions. I can’t tell what the execution context of any of your calls to `MessageProvider` is because you seem to have a property name that collides with the class name.

You are also saying that the problem is that “the page is not being updated”, but I don’t see the page template, so have no idea how anything is supposed to be displayed.

Finally, please give proper types to things and stop abusing `any`. In this case, it appears all of your `any`s can simply be replaced by `string`.

---

<div class="post-metadata">

**Author:** ![ashley\_shookhye](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ashley\_shookhye](https://forum.ionicframework.com/u/ashley_shookhye)\
**Post date:** [June 15, 2017, 2:33am UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/3 "2017-06-15T02:33:09Z")

</div>

Hello,  
The template is as per below

```auto
  <p>
    {{message}}
  </p>

```

Just for testing purposes I have named the property same as the class name as below

```auto
import { MessageProvider } from './../../providers/message/message';
import { Page1Page } from './../page1/page1';
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Subscription } from 'rxjs/Subscription';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  message: any;
  subscription: Subscription;
  constructor(public navCtrl: NavController, private MessageProvider: MessageProvider) {
    
  }

  ionViewDidLoad() {
    this.subscription = this.MessageProvider.getMessage().subscribe(message => {
      console.log(message); 
      this.message = message; 
    });
  }

}

```

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [June 15, 2017, 4:00am UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/4 "2017-06-15T04:00:27Z")

</div>

So would you kindly address the issues in my previous post and eliminate the name clashes and abuse of `any`?

---

<div class="post-metadata">

**Author:** ![ashley\_shookhye](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ashley\_shookhye](https://forum.ionicframework.com/u/ashley_shookhye)\
**Post date:** [June 15, 2017, 4:51am UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/5 "2017-06-15T04:51:10Z")

</div>

Hello,

thanks for the reply. I have addressed the issues you mentioned. But still when i toggle the wifi connection on my phone, the text is not updated on the page even though the console prints the value.

Thanks for helping,  
Ashley

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [June 15, 2017, 6:17am UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/6 "2017-06-15T06:17:27Z")

</div>

> [@ashley\_shookhye](#):
>
> I have addressed the issues you mentioned

Would you mind posting the updated code?

---

<div class="post-metadata">

**Author:** ![ashley\_shookhye](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ashley\_shookhye](https://forum.ionicframework.com/u/ashley_shookhye)\
**Post date:** [June 15, 2017, 7:01am UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/7 "2017-06-15T07:01:54Z")

</div>

Yes Sure,

```auto
import { MessageProvider } from './../../providers/message/message';
import { Page1Page } from './../page1/page1';
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Subscription } from 'rxjs/Subscription';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  message: string;
  subscription: Subscription;
  constructor(public navCtrl: NavController, private messageService: MessageProvider) {
    
  }

  ionViewDidLoad() {
    this.subscription = this.messageService.getMessage().subscribe(message => {
      console.log(message); 
      this.message = message; 
    });
  }

}

```

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [June 15, 2017, 8:35am UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/8 "2017-06-15T08:35:14Z")

</div>

And what does the app component look like now?

---

<div class="post-metadata">

**Author:** ![ashley\_shookhye](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ashley\_shookhye](https://forum.ionicframework.com/u/ashley_shookhye)\
**Post date:** [June 20, 2017, 4:33pm UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/9 "2017-06-20T16:33:23Z")

</div>

Hello,

Apologies for replying so late. I have found a solution to my problem. Grateful to have your advice. See code below using NgZone

```auto
this.subscription = this.messageService.getMessage().subscribe(message => {
     this._zone.run(() => {
      this.message = message; 
      });
    });

```

Ashley

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [June 20, 2017, 8:39pm UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/10 "2017-06-20T20:39:39Z")

</div>

No, if you are directly interacting with zones, something is fundamentally broken.

---

<div class="post-metadata">

**Author:** ![ashley\_shookhye](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ashley\_shookhye](https://forum.ionicframework.com/u/ashley_shookhye)\
**Post date:** [June 21, 2017, 4:46am UTC](https://forum.ionicframework.com/t/update-ui-based-on-network-status/94350/11 "2017-06-21T04:46:49Z")

</div>

Ok. If you don’t mind me asking, can you point in the direction how I can troubleshoot this part?

Ashley
