controller:
loginModule.controller("loginController", ['$window', '$rootScope', '$scope', '$location', 'authenticationService', function($window, $rootScope, $scope, $location, authenticationService){
$scope.newclass = false;
$scope.login = function(){
if($scope.form_login.$invalid){
return false;
}else{
$scope.newclass = true; //按钮样式
$scope.dataloading = true; //ng-disabled禁用按钮
authenticationService.login($scope.username, $scope.password, function(response){
if(response.data.status_code == 200){
$window.location.href = 'view/home.html';
}else if{
//其他错误状态
}
});
}
};
}]);
service:
loginModule.factory('authenticationService', ['$http', '$base64', '$cookieStore', '$rootScope', '$timeout', function($http, $base64, $cookieStore, $rootScope, $timeout){
var service = {};
service.login = function(username, password, callback){
$http({
method: 'POST',
url: 'services/login',
data: {usrname: username, password: password}
}).then(function(response){
callback(response);
},
function(response){
alert('connection error');
//这里想要改变controller里面newcalss和dataloading的值???
})
}
return service;
}])
初学,在写一段用户登陆的功能,controller和service如上,我在controller里面定义了dataloading和newclass两个值来控制提交按钮的样式和禁用。
请问在service里HTTP请求错误后(注释处)想改变newclass和dataloading的值来控制按钮禁用应该怎么做?
大家讲道理2017-04-10 16:53:38
factory创建的服务应该只关注请求,把你的authenticationService注入到loginController中,在loginController里调用service.login传入回调函数,再在回调函数里改变$scope下的值