如何限制angularjs中的异步调用?

时间:2016-02-23 11:37:54

标签: javascript angularjs

这是我的代码

.state('appSetting', { abstract: true, url: '/appSetting/:appId', templateUrl: appUrl + '/AppSetting/Index', controller: 'AppSettingCtrl' })
.state('appSetting.majorObjectList', { url: '/majorObject', templateUrl: appUrl + '/MajorObject/List', controller: 'MajorObjectsCtrl' })


MajorObjectsCtrl = [
  '$scope', '$state', 'uiGridConstants', '$stateParams', '$rootScope', 'dataFactory', 'cacheKeys', '$window', 'ngDialog', '$filter', function ($scope, $state, uiGridConstants, $stateParams, $rootScope, dataFactory, cacheKeys, $window, ngDialog, $filter) {
      "use strict";
     $scope.loadMajorObjects = function () {
          $scope.getData();
          $scope.objectsList.data = $scope.filterData;
      };
    
    }];


AppSettingCtrl = [
  '$scope', '$state', '$stateParams', '$window', '$sessionStorage', 'dataFactory', 'ngDialog', '$q', function ($scope, $state, $stateParams, $window, $sessionStorage, dataFactory, ngDialog, $q) {
      "use strict";
    $scope.getData = function () {

          var appId = $scope.currentAppId;
          var type = $scope.majorObject.type;
          if (_.isEmpty(appId)) {
              return;
          }
          var deferred = $q.defer();
          //   var cacheKey = { key: cacheKeys.objectTypeList($scope.asCacheOptions({ ObjectType: type })) };
          dataFactory.get("/MajorObject/All?applicationId=" + $scope.currentAppId + "&type=" + type)
                 .then(function (result) {
                     $scope.selectAppSetting('Major Objects');
                     $scope.filterData = result.data.data;
                     deferred.resolve($scope.filterData);
                     
                 });
                 return deferred.promise;
      };
    }];
 <div ng-controller="AppSettingCtrl" class="row appReady">
            @RenderPage("_TopNavBar.cshtml")
            <button id="myCheck" ng-click="showSearchPopUp()" class="btn btn-primary"><i class="fa fa-plus"> <span>Search</span> </i> </button>
        </div>

在上面的代码中,来自getData()的{​​{1}}代码。在控制转到majorObjectCtrl appSettingCtrl函数后,立即转移到getData(),然后从majorObjectCtrl中的服务加载数据。但是,在我希望转移到appSettingCtrl

的服务加载数据之后

2 个答案:

答案 0 :(得分:1)

您应该从getData返回承诺。然后在loadMajorObjects中,您可以使用.then将其链接起来以使用$scope.filterData

以下是getData的改进。

$scope.getData = function () {
      var appId = $scope.currentAppId;
      var type = $scope.majorObject.type;
      if (_.isEmpty(appId)) {
          return $q.when();
      }
      //   var cacheKey = { key: cacheKeys.objectTypeList($scope.asCacheOptions({ ObjectType: type })) };
      dataFactory.get("/MajorObject/All?applicationId=" + $scope.currentAppId + "&type=" + type)
             .then(function (result) {
                 $scope.selectAppSetting('Major Objects');
                 return result.data.data;
             });
  };

loadMajorObjects

  $scope.loadMajorObjects = function () {
      $scope.getData().then(function(filterData) {
           $scope.objectsList.data = filterData;
      });
  };

答案 1 :(得分:0)

$scope.loadMajorObjects = function () {
    $scope.getData().then(function (result) {
        $scope.objectsList.data = result;
    });
};