我尝试从控制器中的服务处理成功/错误处理。
我已经试过了:
服务内容:
.factory('AuthenticationService',
['Base64', '$http', '$cookieStore', '$rootScope',
function (Base64, $http, $cookieStore, $rootScope) {
var service = {};
service.Login = function (username, password, callback) {
var authdata = Base64.encode(username + ':' + password);
$rootScope.globals = {
currentUser: {
username: username,
authdata: authdata
}
};
$http.defaults.headers.common['Authorization'] = 'Basic ' + authdata;
$cookieStore.put('globals', $rootScope.globals);
$http.post('http://localhost:8080/v1/login', { username: username, password: password })
.success(function (response) {
callback(response);
});
};
service.ClearCredentials = function () {
$rootScope.globals = {};
$cookieStore.remove('globals');
$http.defaults.headers.common.Authorization = 'Basic ';
};
return service;
}])
和控制器:
.controller('LoginController',
['$scope', '$rootScope', '$location', 'AuthenticationService',
function ($scope, $rootScope, $location, AuthenticationService) {
// reset login status
AuthenticationService.ClearCredentials();
$scope.login = function () {
$scope.dataLoading = true;
AuthenticationService.Login($scope.username, $scope.password, function (response) {
if(response.success) {
$location.path('/');
} else {
$scope.error= response.message;
$scope.dataLoading = false;
}
});
};
}]);
如果我尝试使用警报,它将仅显示在服务中,而不显示在控制器中。
试试这个
.factory('AuthenticationService',
['Base64', '$http', '$cookieStore', '$rootScope',
function (Base64, $http, $cookieStore, $rootScope) {
var service = {};
this.Login = function (username, password) {
var authdata = Base64.encode(username + ':' + password);
$rootScope.globals = {
currentUser: {
username: username,
authdata: authdata
}
};
$http.defaults.headers.common['Authorization'] = 'Basic ' + authdata;
$cookieStore.put('globals', $rootScope.globals);
$http.post('http://localhost:8080/v1/login', { username: username, password: password })
.success(function (response) {
return response;
});
};
this.ClearCredentials = function () {
$rootScope.globals = {};
$cookieStore.remove('globals');
$http.defaults.headers.common.Authorization = 'Basic ';
};
}])
调节器
.controller('LoginController',
['$scope', '$rootScope', '$location', 'AuthenticationService',
function ($scope, $rootScope, $location, AuthenticationService) {
// reset login status
AuthenticationService.ClearCredentials();
$scope.login = function () {
$scope.dataLoading = true;
var response = AuthenticationService.Login($scope.username, $scope.password);
if(response.success) {
$location.path('/');
} else {
$scope.error= response.message;
$scope.dataLoading = false;
}
};
}]);
使用诺言:
.factory('AuthenticationService',
['Base64', '$http', '$cookieStore', '$rootScope', '$q',
function (Base64, $http, $cookieStore, $rootScope, $q) {
var service = {};
service.Login = function (username, password, callback) {
var deferred = $q.defer();
var authdata = Base64.encode(username + ':' + password);
$rootScope.globals = {
currentUser: {
username: username,
authdata: authdata
}
};
$http.defaults.headers.common['Authorization'] = 'Basic ' + authdata;
$cookieStore.put('globals', $rootScope.globals);
$http.post('http://localhost:8080/v1/login', { username: username, password: password })
.then(function (response) {
deferred.resolve(response);
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
};
service.ClearCredentials = function () {
$rootScope.globals = {};
$cookieStore.remove('globals');
$http.defaults.headers.common.Authorization = 'Basic ';
};
return service;
}])
还有你的控制器
.controller('LoginController',
['$scope', '$rootScope', '$location', 'AuthenticationService',
function ($scope, $rootScope, $location, AuthenticationService) {
// reset login status
AuthenticationService.ClearCredentials();
$scope.login = function () {
$scope.dataLoading = true;
AuthenticationService.Login($scope.username, $scope.password)
.then(function(success) {
$location.path('/');
}, function(error) {
$scope.error= response.message;
$scope.dataLoading = false;
});
};
}]);
无需使用$q.defer()
来制造诺言,因为$ http服务已经返回了诺言。 避免使用此Q Defer Anti-Pattern 。
//AVOID this $q.defer ANTI-PATTERN
var deferred = $q.defer();
$http.post('http://localhost:8080/v1/login', { username: username, password: password })
.then(function (response) {
deferred.resolve(response);
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
而是简单地使用$ http服务返回的承诺:
//INSTEAD return promise from $http
var promise = $http.post('http://localhost:8080/v1/login', { username: username, password: password });
return promise;