我知道 Angular 可以处理 Controller 内部的 promise 。例如:
function MyCtrl($scope) {
$scope.myvar = getDeferredPromise();
}
主 Angular 摘要循环优雅地处理这个问题,将延迟函数最终返回的任何值分配给 myvar。
但是,尽管 $http.get() 方法返回一个 promise ,但我无法让它以这种方式工作。例如:
function MyCtrl($scope, $http) {
$scope.myvar = $http.get('/url');
}
get 方法返回的“promise”有一个 success 方法,该方法采用一个函数,该函数被分配了希望分配给 myvar< 的数据/strong>.
但是,它还有一个 then 方法 - 但它给出了整个响应对象 - 而不仅仅是该数据部分!这似乎最终被分配给 myvar!
这个 fiddle 可能有帮助:http://jsfiddle.net/QKnNC/1/
我在这里做错了什么吗?或者这是某种“设计使然”?
最佳答案
The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise with two $http specific methods: success and error.
$http
返回一个 Promise,因此您需要链接 then
来获取数据。
IPService.getV1().then(function (response) {
console.log(response)
$scope.value1 = response.data;
});
关于javascript - Angular JS $http Promise 的行为是否像真正的 $q Promise 一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18660128/