title: angularjs Ionic and global variables: best practice to make a variable available globally
body: <p>I am new to Angular/Ionic.
  Before using Angular/Ionic, at the launch of my app, I was checking if we were under Phonegap or a browser using and storing this information in a global boolean variable and then checking if the app was online or offline and storing it to a global variable too, like this :</p>
  
  <pre><code>var isPhoneGap;
  var connectionStatus;
  isPhoneGap = checkIfPhoneGap();
  
  //later in the code :
  
  connectionStatus = checkIfOnline();
  
  function checkIfPhoneGap() {
      var app = document.URL.indexOf( 'http://' ) === -1 &amp;&amp; document.URL.indexOf( 'https://' ) === -1; // &amp;&amp; document.URL.indexOf( 'file://' );
      if ( app ) {
          return true;
      } else {
          return false;
      }
  }
  function checkIfOnline() {  
      if ( isPhoneGap ) {
          if (checkConnection() == "none" ) {
              connectionStatus = 'offline'; 
          } else {
              connectionStatus = 'online';
          }
          function checkConnection() {
              var networkState = navigator.network.connection.type;
              var states = {};
              states[Connection.UNKNOWN]  = 'Unknown connection';
              states[Connection.ETHERNET] = 'Ethernet connection';
              states[Connection.WIFI]     = 'WiFi connection';
              states[Connection.CELL_2G]  = 'Cell 2G connection';
              states[Connection.CELL_3G]  = 'Cell 3G connection';
              states[Connection.CELL_4G]  = 'Cell 4G connection';
              states[Connection.NONE]     = 'No network connection';
              //console.log('Connection : ' + Connection);
              //console.log('Connection type: ' + states[networkState]);
              return networkState;
          }
      } else {
          connectionStatus = navigator.onLine ? 'online' : 'offline';
      }
      return connectionStatus;
  }
  </code></pre>
  
  <p>Now I would like to do the same with Angular/Ionic, I understand that I have to use a "Service". But is it the best way to make this information available through all the code ?</p>
  
  <p>I am doing the following, but is it the "best practice" ?</p>
  
  <p>in index.html : </p>
  
  <pre><code>&lt;script src="js/app.js"&gt;&lt;/script&gt;
  &lt;script src="js/controllers.js"&gt;&lt;/script&gt;
  &lt;script src="js/services.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>in services.js : </p>
  
  <pre><code>angular.module('SnowBoard.services', [])
  
  .factory('isPhoneGap', function() {
  
      var appp = document.URL.indexOf( 'http://' ) === -1 &amp;&amp; document.URL.indexOf( 'https://' ) === -1; // &amp;&amp; document.URL.indexOf( 'file://' );
      if ( appp ) {
          return true;
      } else {
          return false;
      }
  
  })
  
  ;
  </code></pre>
  
  <p>in app.js :</p>
  
  <pre><code>angular.module('SnowBoard', ['ionic', 'SnowBoard.controllers', 'SnowBoard.services'])
  
  .run(["isPhoneGap","$ionicPlatform", function(isPhoneGap, $ionicPlatform) {
    $ionicPlatform.ready(function() {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      if(window.cordova &amp;&amp; window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      }
      if(window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleDefault();
      }
    });
  
    //CHECK IF ONLINE
    connectionStatus = checkIfOnline(isPhoneGap);
  
    //DEBUG
    //var debugOptionUseLocalDB=0;
    //updateProDB(connectionStatus, debugOptionUseLocalDB);
  }])
  .config(function($stateProvider, $urlRouterProvider) {
  //...all state configurations
  })
  .config(function($stateProvider, $urlRouterProvider) {
  //...
  });
  </code></pre>
  
  <p>This works for now, but I need the boolean <code>isPhoneGap</code> to be available everywhere I need it (almost everywhere in my app).</p>
  
  <p>Can you converge to the best practice to do this ?</p>
  
  <p>Thanks</p>