[英]How to get promise result using AngularJS
The following is the controller used to retrieve information from sharepoint.以下是用于从 sharepoint 中检索信息的 controller。 I can see debugging that the entry
data.d.UserProfileProperties.results[115].Value
has a property value that I need to render in view.我可以看到调试条目
data.d.UserProfileProperties.results[115].Value
具有我需要在视图中呈现的属性值。 How can I get that value from the result promise?如何从结果 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.
任何帮助,将不胜感激。
Instead of using jQuery .ajax
, use the $http service:不要使用 jQuery
.ajax
,而是使用 $http 服务:
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;
}
Then extract the data from the returned promise:然后从返回的 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. $http 服务返回的承诺与 AngularJS 框架集成。 Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc.
只有在 AngularJS 执行上下文中应用的操作才能受益于 AngularJS 数据绑定、异常处理、属性监视等。
For more information, see有关详细信息,请参阅
assign your response object to $scope
object将您的响应 object 分配给
$scope
object
function onSuccess(data) {
$scope.promiseData = data
dfd.resolve(data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.