仅在另一个http服务中收到承诺后才触发http服务

时间:2016-04-29 16:15:28

标签: angularjs

在我的控制器中,我有两个ajax服务:

    inspectionReviewServices.getValue($scope.object.Id).then(function (result) {
        $scope.inspectionReviews = result.data;
        $scope.inspectionReviewsOriginal = angular.copy(result.data);
    }, function (err) {});



damageEventServices.getValuesByInspReviews($scope.inspectionReviews).then(function (result) {
        $scope.damageEvants = result.data;
    })

加载页面时触发的http服务。

第一项服务被解雇inspectionReviewServices,第二项服务被damageEventServices

问题是当damageEventServices触发参数$scope.inspectionReviews

undefined,因为http服务是非同步的,并且未收到inspectionReviewServices中的承诺。

所以我的问题是如何在damageEventServices收到承诺后才触发此inspectionReviewServices服务?

2 个答案:

答案 0 :(得分:2)

inspectionReviewServices.getValue($scope.object.Id).then(function (result) {
    $scope.inspectionReviews = result.data;
    $scope.inspectionReviewsOriginal = angular.copy(result.data);
    damageEventServices.getValuesByInspReviews($scope.inspectionReviews).then(function (damageResult) {
        $scope.damageEvants = damageResult.data;
    })
}, function (err) {});

在独立决议中调用依赖者。

答案 1 :(得分:0)

信守承诺:

promiseReturningService1().then(function(result) {
    //resolved
    promiseReturningService2(result).then(successCallBack, errorCallBack);
    //etc.. etc.. etc...
}, function(error) {
    //rejected
});

//code below this will not wait for the promise executions
alert("hello, I didn't wait!");