0

I want to put in this kind of code is user connected to network or not. If is connected to do this then blocks or if its NOT connected to do something other, such as get data from local storage. I'm confused how to do that.

    getStorageWarents() {

      this.storage.ready()
        .then(() => {
          this.warrentsInStorage = JSON.parse(localStorage.getItem('__mydb/_ionickv/DodNalog'));
          console.log("Warrents in storage: ", this.warrentsInStorage);

        })
        .then(() => {
          this.getAssignedWarrents();
        })
        .then(() => {
          this.getAllGoods();
        })
        .then(() => {
          this.getAllCancelTypes();
        })
        .then(() => {
          this.getAllDeviceTypes();
        })
        .then(() => {
          this.getAllManufacturers();
        })
        .then(() => {
          this.getAllIntereventionTypes();
        })


  }
Pero Peric
  • 105
  • 2
  • 9
  • 1
    You also have the [MDN Documentation](https://developer.mozilla.org/fr/docs/WebAPI/Network_Information) for modern browsers –  Jun 04 '19 at 08:14

1 Answers1

0

You can use navigator global object.

getStorageWarents() {
    isOnline: boolean = navigator.onLine;

     if(isOnline){
       // code block
     } else {
       // get data from local storage
     }
 }

Please follow this article

You can also use ng-connection-service

import { ConnectionService } from 'ng-connection-service';

  isOnline:boolean;
  isConnected = true;

  constructor(private connectionService: ConnectionService) {
    this.connectionService.monitor().subscribe(isConnected => {
      this.isConnected = isConnected;
      if (this.isConnected) {
        this.isOnline = true;
      }
      else {
        this.isOnline = false;
      }
    })
  }


getStorageWarents() {

     if(this.isOnline){
       // code block
     } else {
       // get data from local storage
     }
 }
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
  • and how to implement them into this my code and how to do else if not connected ? Thanks – Pero Peric Jun 04 '19 at 08:19
  • Edited the answer. Please check – Adrita Sharma Jun 04 '19 at 08:24
  • please look at this code for ion-refresher while internet is ON or OFF. http://dpaste.com/2KK9RH9 it didnt work fine. When i disconnect my net and plug net cable...this function also did ELSE statement. Whats wrong ? – Pero Peric Jun 04 '19 at 09:16