如何防止来自不同功能的多重承诺

时间:2015-08-19 14:47:23

标签: angularjs angular-promise

在我的angular-app中,我有3个查询,每个查询都取决于其他查询。我将promise()放在每个人身上。在这种情况下,我怎样才能获得所有这些依赖的承诺$q.all

有人可以为我澄清这个吗?

这是我的代码:

"use strict";

angular.module("tcpApp")
.controller("projectSummaryController", 

    ['$scope', '$routeParams', '$location', 'server', 'modalService', '$q',

    function ( $scope, $routeParams, $location, server, modalService, $q ) {


        $scope.projectId = $routeParams.projectId;
        $scope.subProjectId = $routeParams.subProjectId;
        $scope.phaseId = 0;
        $scope.disciplineId = 0;
        $scope.contractorId = 0;

        $scope.queryConractorInfo = function ( contractorId ) {

            server.contractorInfo.get({

                projectId:$scope.projectId, 
                subProjectId : $scope.subProjectId,
                contractId : $scope.contractorId,
                disciplineId : $scope.disciplineId,
                staticId : 0 /* at present static */

            }).$promise.then(function ( contractorInfo ) {

                $scope.contractorInfo = contractorInfo;
            })
        }

        $scope.queryConractorList = function ( phaseId, disciplineId ) {

        var base = 'http://azvsptcsdev02:678/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/Contracts/'

        console.log( base+$scope.projectId+'/'+$scope.subProjectId+'/'+phaseId+'/'+disciplineId );

        server.contractorsList.query(
                {
                    projectId:$scope.projectId,
                    subProjectId : $scope.subProjectId, 
                    phaseId : phaseId, 
                    disciplineId: disciplineId

                }).$promise.then(function ( contractorsList ) {

                    $scope.contractorId = contractorsList[0].Id; //setting the first contractor as default;

                    $scope.queryConractorInfo( $scope.contractorId );

                });

        }

        $scope.queryProject = function ( prjId, subPrjId ) {

            server.projectSummary.get({id:$scope.projectId})

            .$promise.then(function (data) {

                //only setting phase id and desciple id to get list of contractors

                $scope.phaseId = data.PhaseIds[0].Id; //setting the first phase as default;
                $scope.disciplineId = data.DisciplineIds[0].Id; //setting the first descipline as default;

                $scope.queryConractorList( $scope.phaseId, $scope.disciplineId );


            });

        }

        if($scope.projectId) {

            $scope.queryProject();

        }

        $q.all([ $scope.queryProject, $scope.queryConractorList, $scope.queryConractorInfo ])
        .then(function ( data1, data2, data3 ) {

            console.log( data1, data2, data3 );// i get nothing here!

        })

    }]);

处理此问题的最佳做法是什么?

0 个答案:

没有答案