简体   繁体   English

如何检查离子按钮上的互联网连接?

[英]how to check internet connection on button tap in ionic?

i'm trying to check for an internet connection once the login button is tapped but so far when the button is tapped, the app doesnt go through the next page but it also doesnt display the alert box. 我试图在登录按钮被点击后检查互联网连接,但是到目前为止,当点击按钮时,该应用程序不会通过下一页,但它也不会显示警报框。 how to do it in ionic/angularjs? 如何在ionic / angularjs中做到这一点? here's my code: 这是我的代码:

 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');
}

Use the below function 使用以下功能

$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();
                        }
                    });
                }
            }
        });

Create a factory that would handle all connectivity issue. 创建一个处理所有连接问题的工厂。 Install cordova netwrok plugin to get more data like GPS, WiFi, to handle device and browser connectivity separately 安装cordova netwrok插件以获取更多数据,例如GPS,WiFi,以分别处理设备和浏览器的连接

cordova plugin add cordova-plugin-network-information cordova插件添加cordova-plugin-network-information

Include this factory in your controller and call ConnectivityMonitor.isOnline() to check if the user is connected to internet. 在控制器中包括该工厂,然后调用ConnectivityMonitor.isOnline()来检查用户是否已连接到Internet。

or Use ConnectivityMonitor.startWatching() to monitor the connectivity 或使用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);  
        }       
    }
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM