【发布时间】:2016-03-08 16:20:29
【问题描述】:
我在 Angular 中链接 Promise 时遇到了一些问题。我想做的是从 API 中获取我的项目对象,然后检查项目所有者是否有任何容器,如果有,则触发另一个 GET 来检索容器。最后分配给scope 的容器应该为空或从API 检索到的对象。
现在,下面的示例立即解析为第二个 then 函数,我收到错误 TypeError: Cannot read property 'owner' of undefined。我做错了什么?
$http.get('/api/projects/' + id).then(function (data) {
$scope.project = data.project;
return data.project;
}).then(function (project) {
var containers = project.owner.containers;
if (containers.length) {
return $http.get('/api/containers/' + containers[0]);
} else {
return null
}
}).then(function (container) {
$scope.container = container;
});
【问题讨论】:
标签: angularjs promise angular-promise