【发布时间】:2016-08-15 03:52:55
【问题描述】:
我创建了这个 http 服务:
(function () {
"use strict";
angular.module("inspectionReview").factory("inspectionReviewServices", ["$http", "config", inspectionReviewServices]);
function inspectionReviewServices($http, config) {
var serviceAddress = config.baseUrl + "api/InspectionReview/";
var service = {
getValues: getValues
};
return service;
function getValues(objectId, frequencyId) {
return $http.get(serviceAddress + "getValuesByObjectTypeId/" + objectId + "/" + frequencyId);
}
}
})();
这里我在控制器中使用它来获取数据:
function checkInspection() {
var frequencyId = 5;
inspectionReviewServices.getValues($scope.object.Id, frequencyId).then(function (result) {
$scope.someData = result.data;
});
}
当我调用函数 checkInspection 时,我需要等到 $scope.someData 属性被数据填充,并且只有在它被数据填充之后才能执行更多行。目前我得到了进一步执行的承诺和代码。
编辑(根据达伦的回答):
我更改了调用服务的服务和函数:
function getValues2(objectId, frequencyId) {
return $http.get(serviceAddress + "getValuesByObjectTypeId/" + objectId + "/" + frequencyId).then(function (result) {
return result.data;
});
}
这是在控制器中调用服务的函数:
function checkInspectionReviewValidety() {
var frequencyId = 5;
return inspectionReviewServices.getValues2($scope.object.Id, frequencyId)
}
但我仍然没有得到想要的结果。
如何更改 checkInspection 函数以使其等到 $scope.someData 属性被数据填充?
【问题讨论】:
-
好吧,你不能。将需要
result.data的逻辑放在then块中。 -
你必须把你想做的工作作为一个函数传入。