对不起,Angular.JS / JS / Ionic很新,一直在使用这个登录控制器,并且有一个创建注册控制器+服务的任务,任何关于使用$ scope.data中的信息来解决这个问题的建议对于var ie userObj,可以在其他控制器/服务中使用。**编辑
控制器:
.controller('LoginCtrl', function($scope, LoginService, $ionicPopup, $state) {
$scope.data = {};
$scope.login = function() {
LoginService.loginUser($scope.data.username, $scope.data.password).success(function(data) {
$state.go('tab.dash');
}).error(function(data) {
var alertPopup = $ionicPopup.alert({
title: 'Login failed!',
template: 'Please check your credentials',
okType: 'button-assertive', // String (default: 'button-positive'). The type of the OK button.
});
});
}
})
服务:
.service('LoginService', function($q) {
return {
loginUser: function(uName, uPass) {
var deferred = $q.defer();
var promise = deferred.promise;
if (uName == 'user' && uPass == 'secret') { <!-- some def values which are user and secret -->
deferred.resolve('Welcome ' + uName + '!');
} else {
deferred.reject('Wrong credentials.');
}
promise.success = function(fn) {
promise.then(fn);
return promise;
};
promise.error = function(fn) {
promise.then(null, fn);
return promise;
};
return promise;
}
}
})