0

In my angular-app, I have 3 queries, each depends on the others. I am putting promise() to each of them. In this case how can i get $q.all for all this depended promise once done?

Can anyone clarify this for me, please?

Here is my code :

"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!

        })

    }]);

What is the best practice to handle this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2024080
  • 1
  • 14
  • 56
  • 96
  • You should chain your promises. Check [this](http://stackoverflow.com/questions/21999178/removing-nested-promises/22000931#22000931) answer. – AndrasCsanyi Aug 19 '15 at 15:16
  • I am not able to implement for my scenario. any one help me to reproducing my code here? – user2024080 Aug 19 '15 at 18:57

0 Answers0