[英]Angular JS Promise only resolving in markup, not model
I'm having an issue resolving my promise—the strange this is that in my markup: 我在兑现承诺时遇到了一个问题-奇怪的是,我的标记是:
{{details.customer_email}}
It resolves properly , and displays the e-mail address returned by the '$http` request. 它可以正确解析,并显示“ $ http”请求返回的电子邮件地址。
However, attempting to access this: 但是,尝试访问此文件:
$scope.user = {
...
emailAddress : $scope.details.customer_email,
...
};
is null
. 为
null
。
Here's the relevant block: 这是相关的块:
$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
...
};
It's working in your markup is because angular's template engine is "promise aware", it's very convenient. 它在您的标记中起作用是因为angular的模板引擎是“承诺感知”的,所以非常方便。
$q promises are recognized by the templating engine in angular, which means that in templates you can treat promises attached to a scope as if they were the resulting values.
$ qpromise由模板引擎以角度识别,这意味着在模板中,您可以将附加到作用域的promise视为它们的结果值。
In your JavaScript code, however, you have to handle it all by yourself: 但是,在JavaScript代码中,您必须自己处理所有这些:
$scope.user = {
...
emailAddress : null,
...
};
$scope.details.then(function(details) {
$scope.user.emailAddress = details.customer_email;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.