我有以下提供商:
globalLinksApp.factory('sectionStatus', function ($http: ng.IHttpService ) {
return {
currentStatus: function (section) {
let restBaseUrl = Url;
$http
.get(restBaseUrl, { withCredentials: true })
.then(
(response) => {
let apiResult = <ApiResult<[SP.News.SectionStatus]>>(response.data);
let status = apiResult.data;
console.log("$scope.status called from factory = " + status.filter(a => a.section == section)[0].hidden);
return (status.filter(a => a.section == section)[0].hidden);
},
(errorResponse) => {
console.log('ERROR obtaining sections status: ' + JSON.stringify(errorResponse));
});
}
}
});
然后将其注入以下控制器:
globalLinksApp.controller('GlobalFunctionsCtrl', ($scope, sectionStatus, $q: ng.IQService) => {
$scope.webApplicationUrls = webApplicationUrls;
let promotedLinkManager = new SP.PromotedLinks.PromotedLinkManager(SP.WebApplicationUrls.inside);
let listGuid = '1234';
$q.when(promotedLinkManager.getSchillingAppLinks(listGuid))
.then((promotedLinks: [SP.PromotedLinks.PromotedLink]) => {
$scope.promotedLinks = promotedLinks;
$q.when(sectionStatus.currentStatus('GlobalFunctions')).then((a) => {
$scope.status = a;
console.log("scope status in the then block is: " + $scope.status);
});
console.log("$scope.status called from GlobalFunctionsCtrl = "+ $scope.status);
$scope.isThisSectionHidden = ($scope.status == 'No');
console.log("$scope.isThisSectionHidden = " + $scope.isThisSectionHidden);
});
$scope.srDone = () => {
//code
}
});
控制台按以下顺序显示日志消息:
$scope.status called from GlobalFunctionsCtrl = undefined
$scope.isThisSectionHidden = false
scope status in the then block is: undefined
$scope.status called from factory = No
我需要利用提供程序API调用提供的结果,但是,即使在使用then
之后,似乎结果也不会在我希望的时候返回。我需要做些什么才能解决这个问题?
答案 0 :(得分:1)
在链接方面,Promise非常简单。如果结果未显示在下一个then
回调中,或者它似乎没有等待先前的承诺完成,则表示未从先前的then
返回承诺。
应该是:
return $http.get(...)...