AngularJS:在使用数据时延迟从休息服务获取数据导致html问题

时间:2013-10-09 11:05:04

标签: javascript mongodb angularjs

我正在开发一个使用Mongo作为DB和Java的角度应用程序。

当我点击获取数据按钮时,我正在尝试从mongo数据库中获取数据,请参阅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 个答案:

答案 0 :(得分:1)

我假设ConsumptionEngineRESTService.getAll();使用了角度资源吗?

如果是这种情况(并且如果它使用angular的$http服务),则必须在成功回调中处理您的数据。资源和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的文档。 'cards'示例代码,第8行,采用了相同的模式。