2017-02-14 81 views
0

我試圖檢查互聯網連接,一旦登錄按鈕被點擊,但到目前爲止,當按鈕被點擊,應用程序不會通過下一頁,但它也不會顯示警告框。如何在離子/ angularjs中做到這一點?這裏是我的代碼:如何檢查離子按鈕上的互聯網連接?

if (navigator.onLine) { 
    userFactory.getUser(usern).then(function(response) 
    { 
     if(JSON.stringify(response.data) === "null") 
     { 
      alert('Please sign-up for an account.'); 
     } 
     else 
     { 
      if(pass === response.data.Password) 
      { 

       var myPopup = $ionicPopup.show({ 
        template: '<input type="password" ng-model="userdata.passwordChange">', 
        title: 'Change Password', 
        scope: $scope, 
        buttons: [ 
        { text: 'Ok' }, 
        { 
         text:'Cancel', 
         type: 'button-positive', 
         onTap: function(e) 
         { 
          if(e == true) 
          { 
           myPopup.close(); 
          } 
          else 
          { 

           $location.path('/page17'); 
           console.log($location.path()); 
           myPopup.close(); 
          } 

         } 
        } 
        ] 
       }); 
      } 
      else 
      { 
       if(pass == $scope.userdata.passwordChange) 
       { 
        $location.path('/page9'); 
       } 
       else if(pass == "omar_1992!") 
       { 
        $location.path('/page9'); 
       } 
       else 
       { 
        alert('Login failed. Please check your credentials.'); 
       } 

      } 
     } 

    }); 
}else{ 
    alert('no internet connection'); 
} 

回答

0

創建一個工廠,將處理所有的連接問題。安裝科爾多瓦netwrok插件獲得更多的像GPS,WIFI,數據處理設備和瀏覽器連接單獨

科爾多瓦插件添加科爾多瓦 - 插件網絡信息

包含這個工廠在控制器和呼叫ConnectivityMonitor.isOnline()檢查用戶是否連接到互聯網。

或使用ConnectivityMonitor.startWatching()監視連接

app.factory('ConnectivityMonitor', function($rootScope, $cordovaNetwork){ 
    var noInternetPopUp; 
    return { 
    isOnline: function(){ 
     if(ionic.Platform.isWebView()){ 
     return $cordovaNetwork.isOnline();  
     } else { 
     return navigator.onLine; 
     } 
    }, 
    ifOffline: function(){ 
     if(ionic.Platform.isWebView()){ 
     return !$cordovaNetwork.isOnline();  
     } else { 
     return !navigator.onLine; 
     } 
    }, 
    startWatching: function(){ 
     if(ionic.Platform.isWebView()){ 

      $rootScope.$on('$cordovaNetwork:online', function(event, networkState){ 

      alert('Network Change : Online'); 
      }); 

      $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){ 

      alert('Network Change : Offline'); 
      }); 

     } 
     else { 

      window.addEventListener("online", function(e) { 

      alert('Network Change : Online'); 
      }, false);  

      window.addEventListener("offline", function(e) { 
       alert('Network Change : Offline'); 
      }, false); 
     }  
    } 
    } 
}) 
0

使用以下功能

$scope.checkInternetConnection=function($ionicPlatform, $ionicPopup) { 
     $ionicPlatform.ready(function() { 
      if(window.Connection) { 
       if(navigator.connection.type == Connection.NONE) { 
        $ionicPopup.confirm({ 
         title: "Internet Disconnected", 
         content: "The internet is disconnected on your device." 
        }) 
        .then(function(result) { 
         if(!result) { 
          ionic.Platform.exitApp(); 
         } 
        }); 
       } 
      } 
     }); 
+0

您好,我嘗試過,但彈出框犯規露面.. –

+0

什麼是你得到的錯誤? – Aravind