我已经安装了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.');
}
}
}
});
}
答案 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)