【问题标题】:AngularJS loading Directives before completing Service http callAngularJS 在完成服务 http 调用之前加载指令
【发布时间】:2014-01-19 22:50:05
【问题描述】:

我尝试通过服务的 http 调用获取数据,并将服务的返回数据分配给控制器的变量 $scope。 fusionJsonObj 并最终将此数据传递给指令,使其在指令范围内可用以进行进一步处理。

问题:

在 http 调用获取数据并将其分配给控制器 $scope.fusionJsonObj 之前,它继续加载指令(此时指令没有与 $scope.fusionJsonObj 关联的数据,因为服务上的承诺没有从服务器 http 调用。

简而言之,请帮助我加载服务 http 并承诺在加载指令之前将 http 数据分配给控制器 $scope.fusionJsonObj

Plunker 链接: http://plnkr.co/edit/xGchsnw2u9LremFATfvY?p=preview

控制器如下:

 angular.module('FundooDirectiveTutorial', [])
      .controller('FundooCtrl', function($scope, readFusionTblService, $window) {
          $scope.rating = 3; //total no of the column
        $scope.listItemIndex=2; //no of column-1    
        alert('## in the Controller - and calling service method on http call');

        var promise = readFusionTblService.getFusionData(); 
        alert('## in the Controller - and calling promise');

        promise.then(
                    function(dataReturn){
                        alert('## in the Controller -  with promise execution');    
                        $scope.fusionJsonObj = dataReturn;

服务如下:

  .service('readFusionTblService', function($http, $q){



        alert("### in the Service ");

        this.getFusionData = function(){
                            var deferred = $q.defer();
                            var urlQuery = "https://www.googleapis.com/fusiontables/v1/query?sql=SELECT%20*%20FROM%201n-NpyEkVKBOLSn_yE2LPL5Tk9K79saDkpKCrF00%20WHERE%20country='Sweden'%20AND%20type='glass'%20ORDER%20BY%20ST_DISTANCE(ComputedAddress,LATLNG(42,-83))%20LIMIT%2025&key=AIzaSyBqKyD7u_2CwBMQ3c9qAeMDTJaQIjYlgJo";

                            alert("### in the Service - before http call");                         
                            $http.get(urlQuery).success(function(data, status) {
                                    alert("### in the Service - After http call ####");                                 
                                    deferred.resolve(data);
                                }).error(function(data, status) {
                                    deferred.reject(data);
                                });                             
                            return deferred.promise;
        }

【问题讨论】:

  • 哦,请将alert替换为console.log
  • 将变更替换为 consloe.log。 (警报是显示调用方法序列)

标签: angularjs


【解决方案1】:

您的某些指令功能依赖于promisepromise.then 总是返回另一个承诺。保持新的承诺并传递给类似的指令

var newPromise =  promise.then(...);
$scope.ratingConfig = {
    myPromise: newPromise
};

HTML 中将承诺传递给指令,如

<div fundoo-rating rating-config="ratingConfig" row-item-index="listItemIndex" rating-value="rating" on-rating-selected="saveRatingToServer(rating, ratingTextVal)" item-data="billTblModel" ></div>

在指令中包装承诺依赖的功能,如

scope.ratingConfig.myPromise.then(function() {/*promise depended code*/});

查看更新的plunker

【讨论】:

  • 礼萨感谢回复。作为新手需要你的帮助。 1) 在加载 mainPage(id="mainPageId") 之前,如何从 $scope.fusionJsonObj 填充“rowDataArray”(现在是硬编码)。 2) 同样在第二页 (id="dashBoardPageId"),如何从 $scope.fusionJsonObj 填充“billTblModel”(现在是硬编码)。简而言之,$scope.fusionJsonObj 将是一种全局数据(通过来自服务的 http 调用),然后填充控制器 $scope.variable,它将在页面加载之前传递给指令......
  • Reza,令人惊讶的是 newPromise 正在发挥作用。如果我想读取 http json 数据的正确方向,请指导我,将其存储到控制器 $scope.fusionJSonObj 中,然后进行进一步处理。
  • $http 它会自动返回promise,因此当您使用$http 时,您不需要$q。如果您的服务器端是RESTful,您也可以使用角度$resource$q,阅读更多详细信息docs.angularjs.org/api/ngResource.$resource
  • 感谢您的回复。作为新手,想与您联系电子邮件 lgframeATyahooDOTcom, Naresh
猜你喜欢
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
  • 2018-07-29
  • 1970-01-01
  • 2015-03-07
  • 2021-11-12
相关资源
最近更新 更多