如何使用Cordova点击按钮检查互联网连接?

时间:2017-02-14 06:50:39

标签: angularjs ionic-framework

我已经安装了cordova网络信息插件,我有以下代码验证登录时的用户。我想在验证用户凭据之前检查互联网连接。如何在离子中做到这一点?

$scope.enterLogin = function(usern,pass)
{
   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.');
            }

         }
     }
        });
}

2 个答案:

答案 0 :(得分:0)

您可以使用navigator

试试这个

$scope.enterLogin = function(usern,pass)
{
    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')
    }
}

答案 1 :(得分:0)

使用Network Information plugin。这可以在设备中正常工作,也可以访问有关网络的一些额外信息。

例如

 $cordovaNetwork.isOnline();