繁体   English   中英

Angular JS Promise仅解析标记,而不解析模型

[英]Angular JS Promise only resolving in markup, not model

我在兑现承诺时遇到了一个问题-奇怪的是,我的标记是:

{{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
        ...
    };

它在您的标记中起作用是因为angular的模板引擎是“承诺感知”的,所以非常方便。

引用自文档

$ qpromise由模板引擎以角度识别,这意味着在模板中,您可以将附加到作用域的promise视为它们的结果值。

但是,在JavaScript代码中,您必须自己处理所有这些:

$scope.user = {
    ...
    emailAddress : null,
    ...
};

$scope.details.then(function(details) {
    $scope.user.emailAddress = details.customer_email;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM