[英]AngularJS - Toaster not working in Controller
Service.js Service.js
this.userLogin = function (username, password) {
var dataBody = $.param({'username': username,'password': password});
return $http({
method: 'POST',
url: servicePathURL,
data: dataBody,
headers: {
"Authorization": "Basic",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function (response) {
$rootScope.globals = {
currentUser: {
username: username,
}
};
return response;
}).catch(function (error) {
throw error;
});
};
Controller.js Controller.js
AuthenticationServiceLogin.userLogin($scope.username, $scope.password)
.then(function (response) {
if (response.status ==200) {
toaster.pop('success', "", "Login Successful");
$location.path('/home');
}
}).catch(function (error) {
toaster.pop('error', "", error.statusText);
});
toaster.pop('error', "", error.statusText);
toaster.pop('error', "", error.statusText);
is not being called when there is an exception while user logs in. $http
method, is there any advantage to returning a $q.defer()
promise rather than an $http
promise or considered as best practice ? $http
方法,返回$q.defer()
承诺而不是$http
承诺或被认为是最佳实践有什么好处吗? If yes, how can I modify above $http
code into promise
? $http
代码修改为promise
? Your code appears to be fine. 您的代码似乎没问题。 So long as you re-throw any errors encountered by your
$http
call, they should propagate all the way up to the controller. 只要您重新抛出
$http
调用遇到的任何错误,它们就应该一直传播到控制器。 I'm inclined to say that any problems with your error handling are not in the code that you've posted. 我倾向于说您处理错误时遇到的任何问题都不在您发布的代码中。
There's no advantage to having a catch handler in service.js if you're not going to do any work with the error thrown. 如果您不打算对抛出的错误进行任何工作,则在service.js中使用catch处理程序没有任何优势。 Service.js will want to look like this:
Service.js将看起来像这样:
Service.js Service.js
this.userLogin = function (username, password) {
return $http({
method: 'POST',
url: servicePathURL,
data: $.param({'username': username,'password': password}),
headers: {
"Authorization": "Basic",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function (response) {
$rootScope.globals = {
currentUser: {
username: username,
}
};
return response;
// The catch block here is useless. The promise returned by $http will transmit any
// errors on its own.
//}).catch(function (error) {
// throw error;
});
};
In response to your second question: there is no advantage to using $q.defer()
instead of returning the promise returned by $http
itself, and a number of major disadvantages - namely that any exceptions thrown by your login method will disappear. 回答您的第二个问题:使用
$q.defer()
而不返回$http
本身返回的承诺没有任何好处,还有许多主要缺点-即您的登录方法抛出的任何异常都将消失。 See The Deferred Anti-pattern here for details: https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns 有关详细信息,请参见此处的延迟反模式: https : //github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns
$http
is definitely the preferred option. $http
绝对是首选。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.