我有点困惑,我在网上阅读的关于$q
和$http
的资源越多,我的脑袋就越多。所以,如果我进行$http.get
来电,那不包含承诺吗?或者我带来$q
?
答案 0 :(得分:1)
它建立在$ q上并返回一个promise。请参阅文档:https://docs.angularjs.org/api/ng/service/ $ http
那里有例子:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
$ http.get只是上面的便捷方法。
答案 1 :(得分:0)
$ http API基于$ q公开的延迟/承诺API 服务。虽然对于简单的使用模式,这并不重要 高级用法,熟悉这些API非常重要 以及他们提供的保证。
https://docs.angularjs.org/api/ng/service/$http
意味着$http.get
无论如何都会返回一个承诺。无需嵌套自己的$q
方法。只需返回$http
调用。
答案 2 :(得分:0)
您可以参考以下链接
https://www.peterbe.com/plog/promises-with- $ HTTP
此服务($ http.get())将返回promise作为成功回调和错误回调...所以此函数本身返回promise。你只需要处理它
答案 3 :(得分:0)
$ http.get来电不包含承诺吗?答案是肯定的,您可以使用$ http
返回承诺或解决承诺从$ http.get
返回承诺getData: function() {
return $http.get('some url'); // you can resolve this promise later
// using (then)
}
稍后在您的代码中,您可以像这样解决上述承诺
...
myService.getData().then(function(response) {
// do something with response
}).catch()
解决内联承诺
getData: function() {
$http.get('some url').then(function(response) {
// do something with response
}).catch()
}