代码之家  ›  专栏  ›  技术社区  ›  Mohamed Hussain

AngularJS:从rest服务获取数据延迟,导致在使用数据时出现html问题

  •  0
  • Mohamed Hussain  · 技术社区  · 11 年前

    我正在开发一个angular应用程序,使用Mongo作为DB和Java。

    当我点击获取数据按钮时,我正试图从mongo DB中获取数据。请参阅js bin中的代码 http://jsbin.com/aRuWInU/2/edit ,对于我的html页面,请参阅下面的代码:

    <!DOCTYPE html>
    <html ng-app="myapp">
    <head>
    <script src="/angular.js"></script>
    <script src="myscript.js"></script>
    <meta charset=utf-8 />
    <title>Demo</title>
    
      </head>
    
    <body ng-controller="MyApp">
        <!--It will fetch data from Mongo Using REST service through 
        the angular factory ConsumptionEngineRESTService-->
        <button type="button" ng-click="getAllData()">Get Data</button>
        <h1>My Demo With filter</h1>
        <div ng-repeat="Maindata in myFilteredKey" class="">
            <h2>{{Maindata}}</h2>
            <div ng-repeat="item in data | filter:{'key':Maindata}">
                <p>My key is {{item.key}}</p>
                <p>My val is {{item.value}}</p>
            </div>
    
        </div>
    
    </body>
    </html>
    

    当我从REST服务获取数据时会发生问题, 获取数据的延迟会导致问题,例如$scope.data未填充,但 在下面的for循环中用于填充$scope.myFilteredKey数组,因此我的$scope.myFilteredKey阵列为空。请参阅myscript.js的代码

    myscript.js
        var app = angular.module('myapp', []);
    
    app.controller('MyApp', ['$scope', function($scope,ConsumptionEngineRESTService) {
            $scope.getAllData=function(){
                console.log("hi");
                /*STUB Data: when it is used , code is working perfectly*/
                  /* $scope.data = [
                    {key:"home",value:"hk1"},
                    {key:"home",value:"hk2"},
                    {key:"home",value:"hk3"},
                    {key:"home",value:"hk4"},
                    {key:"product",value:"pk1"},
                    {key:"product",value:"pk2"},
                    {key:"product",value:"pk3"},
                    {key:"product",value:"pk4"},
                    {key:"service",value:"sk1"},
                    {key:"service",value:"sk2"},
                    {key:"service",value:"sk3"},
                    {key:"service",value:"sk4"}
                ]; */
                /* Data From REST Service: Issue happens when i fetch data from REST service,
                *Delay in fetching data cause me the issue, such as $scope.data is not populated but
                *that is used in the below for loop to populate the $scope.myFilteredKey array*/
                /*ConsumptionEngineRESTService is a angular factory. uses REST service to fetch from Mongo*/
                $scope.data =ConsumptionEngineRESTService.getAll();
                $scope.myFilteredKey=[];
                for(i=0;i<$scope.data.length;i++){
                    if($scope.myFilteredKey.indexOf($scope.data[i].key)==-1){
                        $scope.myFilteredKey.push($scope.data[i].key);
                        console.log($scope.data[i].key);
                    }
                }
            }
    }]);
    

    请帮助我解决这个问题,即当for循环执行时,我想填充$scope.data,因为for循环正在使用$scope.data。 注意:对于这个问题,我没有包括REST服务角度工厂 ConsumptionEngineRESTService .

    app.factory("ConsumptionEngineRESTService ", function($resource, $http) {
      var resource = $resource("/rest/:ID ", { ID : "@_ID " },
        {
          'create':  { method: 'POST' },
          'getAll':   { method: 'GET', isArray: true },
          'get':    { method: 'GET', isArray: false },
          'update':  { method: 'PUT' },
          'destroy': { method: 'DELETE' }
        }
      );
      return resource;
    });
    

    请帮我做这件事。谢谢你提前提供的帮助。

    1 回复  |  直到 11 年前
        1
  •  1
  •   mnemosyn    11 年前

    我认为 ConsumptionEngineRESTService.getAll(); 内部使用角资源?

    如果是这样的话(如果是使用角度 $http 服务),您将不得不在成功回调中处理您的数据。resource和http方法的结果只是承诺,即代理对象,它们最终会包含数据,但不会马上包含。您的代码应该是这样的:

    $scope.data = ConsumptionEngineRESTService.getAll(function() {
        $scope.myFilteredKey=[];
        // note that you can work with $scope.data in here, because it's been 
        // assigned before
        for(i = 0; i < $scope.data.length; i++) {
            if($scope.myFilteredKey.indexOf($scope.data[i].key)==-1){
                $scope.myFilteredKey.push($scope.data[i].key);
                console.log($scope.data[i].key);
            }
        }
    });
    

    如果你 ConsumptionEngineRESTService 还不支持此回调,我建议您仔细查看的文档 angular-resource “卡片”示例代码第8行采用了完全相同的模式。