Skip to content Skip to sidebar Skip to footer

How To Get Promise Result Using Angularjs

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 valu

Solution 1:

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

functiongetCurrentUserData() {
    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;               
} 

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();           

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.

For more information, see

Solution 2:

assign your response object to $scope object

function onSuccess(data) {            
    $scope.promiseData = data
    dfd.resolve(data);                    
}

Post a Comment for "How To Get Promise Result Using Angularjs"