我遇到了解决我的承诺的问题 - 奇怪的是,这是我的标记:
{{details.customer_email}}
它正确解析 ,并显示'$ http`请求返回的电子邮件地址。
但是,尝试访问此内容:
$scope.user = {
...
emailAddress : $scope.details.customer_email,
...
};
是null
。
这是相关的块:
$scope.session = {
is_authenticated: false,
customer_email: null
};
var detailsDeferred = $q.defer();
$scope.details = detailsDeferred.promise;
$scope.authed = function () {
$http({
url: 'http://api.foo/auth',
withCredentials: true,
method: "GET",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function (data, status, xhr) {
$scope.session = {
is_authenticated: data.is_authenticated,
customer_email: data.customer_email
};
detailsDeferred.resolve($scope.session);
})
...
return $scope.session;
};
$scope.authed();
$scope.user = {
...
emailAddress: $scope.session.customer_email
...
};
答案 0 :(得分:1)
它在你的标记中工作是因为angular的模板引擎是“承诺意识”,它非常方便。
引自doc:
$ q promises被模板化引擎识别为angular,这意味着在模板中,您可以将附加到范围的promise视为结果值。
但是,在您的JavaScript代码中,您必须自己处理:
$scope.user = {
...
emailAddress : null,
...
};
$scope.details.then(function(details) {
$scope.user.emailAddress = details.customer_email;
});