将承诺传递给ngRepeat

scr*_*key 9 angularjs

所以,我看到了一个例子,他们正在将一个angualar传递给ngRepeat并且工作正常.出于某种原因,当我设置此示例时,它不起作用.谁能告诉我为什么?如果您在没有延期的情况下分配数据,它可以正常工作,即$scope.objects = [{id:1}...]
非常感谢
Fiddle

<!doctype html>
<html ng-app="app">
<head>
</head>
<body>

  <testlist/>

  <script src="/lib/angular/angular.js"></script>
  <script>
   var app = angular.module('app', []);

   app.factory('dataService', function ($q) {
     return {
       getData : function () {
         var deferred = $q.defer();
         setTimeout(function () {
           deferred.resolve([{id:1},{id:2},{id:3},{id:4}]);
         },0);
         return deferred.promise;
       }
     };
   });


   app.directive('testlist', ['dataService', function(dataService) {
     return {
        restrict: 'E',
        replace: true,
        scope : {},
        template: '<div ng-repeat="data in objects">{{inspect(data)}}{{data.id}}</div>',
        controller: function($scope) {
          $scope.objects = [{id:1},{id:2},{id:3},{id:4}];
          $scope.inspect = function (obj) {
            console.log(obj)
          }
        }
      }
    }]);

  </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Rai*_*baz 13

我不认为你可以直接使用promise对象,你应该使用文档then中所述的回调.

这意味着你的

$scope.objects = dataService.getData();
Run Code Online (Sandbox Code Playgroud)

应该是类似的东西

dataService.getData().then(function(data) {
    $scope.objects = data;
});
Run Code Online (Sandbox Code Playgroud)

否则,您$scope.objects将包含promise对象,而不是您传递给的数据resolve.

请在此处查看更新的小提琴.

  • 使用`$ parseProvider.unwrapPromises(true)`重新更新小提琴,它按预期工作.将其重新设置为"false"的行为与您的问题相同,而将其设置为"true"则允许将promise对象传递给ng-repeat并在解析时对其进行解析. (3认同)