如何使用AngularJS获得承诺结果 [英] How to get promise result using AngularJS

查看:41
本文介绍了如何使用AngularJS获得承诺结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是用于从共享点检索信息的控制器.我可以看到调试信息,条目data.d.UserProfileProperties.results[115].Value具有我需要在视图中呈现的属性值.如何从结果承诺中获得该价值?

The following is the controller used to retrieve information from sharepoint. I can see debugging that the entry data.d.UserProfileProperties.results[115].Value has a property value that I need to render in view. How can I get that value from the result promise?

(function() {
    'use strict'
    var createPurchasingCardController = function($scope, $rootScope, $filter, $window, $location, $timeout, requestService) {        

        $scope.actionTitle = "";
        $scope.counter = [];                      

        var getCurrentUserData = function () {

            var dfd = new $.Deferred();
            var queryUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
            $.ajax({
                url: queryUrl,
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                success: onSuccess,
                error: onError,
                cache: false
            });

            function onSuccess(data) {            
                dfd.resolve(data);                    
            }

            function onError(data, errorCode, errorMessage) {
                dfd.reject(errorMessage);
            }

            return dfd.promise();               
        }            

        var _init = function () {                
            $scope.counter = getCurrentUserData();
            console.log($scope.counter);
        }

        _init();

    }

    angular.module('myApp').controller('createPurchasingCardController', ['$scope', '$rootScope', '$filter', '$window', '$location', '$timeout', 'requestService', createPurchasingCardController]);
}());

我试图将其放入柜台,但没有显示.任何帮助将不胜感激.

I have tried to get it into the counter but it is not showing up. Any help would be appreciated.

推荐答案

使用$ http服务代替使用jQuery .ajax:

Instead of using jQuery .ajax, use the $http service:

function getCurrentUserData() {
    var queryUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
    var promise = $http({
        url: queryUrl,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        cache: false
    }).then(function(response) {
        return response.data;
    }).catch(function(response) {
        console.log("ERROR", response);
        throw response;
    });

    return promise;               
} 

然后从返回的promise中提取数据:

Then extract the data from the returned promise:

function _init() {                
    var promise = getCurrentUserData();

    promise.then(function(data) {
        $scope.counter = data;
        console.log($scope.counter);
    });     
}

_init();           

$ http服务返回的promise已与AngularJS框架集成.只有在AngularJS执行上下文中应用的操作才能受益于AngularJS数据绑定,异常处理,属性监视等.

The promises returned by the $http service are integrated with the AngularJS framework. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc.

有关更多信息,请参见

这篇关于如何使用AngularJS获得承诺结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆