是否可以从 AngularJS 中的 $http.get 的成功方法中 return 一个值?
Is it possible to return a value from the success method of $http.get in AngularJS?
我现在对承诺有点着迷了。
给定以下代码:
$scope.getData = function(user) {
return $http.post('/endpoint', "some post data", { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.success(function (response) {
var myThing = { stuff: response.infoData };
localStorageService.set('myData', { stuff: response.infoData });
return myThing;
});
};
myThing 对象是 return 从成功回调中编辑的对象:
- returned 作为 $scope.getData 方法的结果,或者
- 被忽视并不知所措?
据我了解:
- 因为 $http.post 是一个承诺,它异步解析然后 运行s 成功方法
- 当成功方法是 运行 时,它 return 是 myThing 对象,但这并没有神奇地替换整个 $http.post 承诺本身
这是正确的吗?有没有我遗漏的东西,在成功方法中 returning 对象有什么好处,还是 return 是多余的? (我很清楚这是可以改进的代码,但我正在尝试具体找出为什么有人会写这个,或者这是否完全是一个错误)
在 $http
方法上使用 success/error
回调不会使您能够 return 从中获取数据。承诺确实提供了这种能力。它目前所写的方式会做它看起来的事情。就像 myThing
不会被当前的实现 return (那是多余的 return 语句)。
Why someone would have written this, or if it was entirely a mistake?
实施此方法的人试图 return 从中获取数据,但我不认为他通过查看当前代码成功了。
利用 return 由 $http
方法提供的承诺,这将使您能够通过异步调用形成承诺链。为此,您需要使用 .then
而不是 .success
方法。
$scope.getData = function(user) {
return $http.post('/endpoint', "some post data", { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.then(function (res) {
var response = res.data;
var myThing = { stuff: response.infoData };
localStorageService.set('myData', { stuff: response.infoData });
return myThing;
});
};
如果您真的有兴趣通过调用 getData
方法来检索数据,那么您可以使用 getData
方法,您应该将 .then
函数置于由它 return 编辑的承诺之上链接它。
$scope.getData().then(function(thing){
console.log(thing);
})
Side Note .success
& error
function over $http
methods are deprecated.
我现在对承诺有点着迷了。
给定以下代码:
$scope.getData = function(user) {
return $http.post('/endpoint', "some post data", { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.success(function (response) {
var myThing = { stuff: response.infoData };
localStorageService.set('myData', { stuff: response.infoData });
return myThing;
});
};
myThing 对象是 return 从成功回调中编辑的对象:
- returned 作为 $scope.getData 方法的结果,或者
- 被忽视并不知所措?
据我了解:
- 因为 $http.post 是一个承诺,它异步解析然后 运行s 成功方法
- 当成功方法是 运行 时,它 return 是 myThing 对象,但这并没有神奇地替换整个 $http.post 承诺本身
这是正确的吗?有没有我遗漏的东西,在成功方法中 returning 对象有什么好处,还是 return 是多余的? (我很清楚这是可以改进的代码,但我正在尝试具体找出为什么有人会写这个,或者这是否完全是一个错误)
在 $http
方法上使用 success/error
回调不会使您能够 return 从中获取数据。承诺确实提供了这种能力。它目前所写的方式会做它看起来的事情。就像 myThing
不会被当前的实现 return (那是多余的 return 语句)。
Why someone would have written this, or if it was entirely a mistake?
实施此方法的人试图 return 从中获取数据,但我不认为他通过查看当前代码成功了。
利用 return 由 $http
方法提供的承诺,这将使您能够通过异步调用形成承诺链。为此,您需要使用 .then
而不是 .success
方法。
$scope.getData = function(user) {
return $http.post('/endpoint', "some post data", { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.then(function (res) {
var response = res.data;
var myThing = { stuff: response.infoData };
localStorageService.set('myData', { stuff: response.infoData });
return myThing;
});
};
如果您真的有兴趣通过调用 getData
方法来检索数据,那么您可以使用 getData
方法,您应该将 .then
函数置于由它 return 编辑的承诺之上链接它。
$scope.getData().then(function(thing){
console.log(thing);
})
Side Note
.success
&error
function over$http
methods are deprecated.