Javascript Angular.js深度对象分辨率

Javascript Angular.js深度对象分辨率,javascript,angularjs,angularjs-ng-repeat,angularjs-digest,Javascript,Angularjs,Angularjs Ng Repeat,Angularjs Digest,这是一个有点宽泛的问题,所以我会尽量简明扼要 在处理应用程序的导入过程时,我遇到了以下数据结构: [ { type: foreignParent, children: [ { type: foreignChild imported: true|false }, { type: foreignChild imported: true|false } ] }

这是一个有点宽泛的问题,所以我会尽量简明扼要

在处理应用程序的导入过程时,我遇到了以下数据结构:

[
  {
    type: foreignParent,
    children: [
      {
        type: foreignChild
        imported: true|false
      },
      {
        type: foreignChild
        imported: true|false
      }
    ]
  }
]
父对象列表由一个API调用提供,父对象的子对象列表由每个父对象1+n的附加API调用提供,导入的更改值基于localChildren对象中是否存在已导入到平台中的内容。foreignParent、foreignChild和localChild是三个不同的对象,由三个不同的服务提供不同的结构

接下来的问题是,是否最好使用此控制器操作中涉及的三个服务来构建一个大型对象数组,并简单地使用ng repeat对其进行迭代:

更简单的第三种选择是使用本机对象,不带函数属性,并将函数放在控制器上:

<ul>
  <li ng-repeat="p in foreignParents">
    <ul>
      <li ng-repeat="getChildren(p)">
        {{isImported(c) ? 'Imported' : 'Not Imported'}}
      </li>
    </ul>
  </li>
</ul>
不幸的是,这并没有按我预期的方式工作,但这是因为我这边的数据问题。我和一些朋友谈过这件事,没有人知道被广泛接受的正确方法是什么,所以我提出把它贴到StackOverflow上,以获得第二种意见


对于这些由多个服务组成的复杂嵌套列表,我不确定哪种方法是正确的,也不确定如何查看列表被消化了多少次,以及函数结果中的更改如何传播。我确信在某些情况下,我可能需要一个明确的$scope.$watch,甚至可能需要强制使用$scope.$digest,但直到现在我还没有发现我必须深入研究watchs或digest。我还没有在野外找到一个足够复杂的示例应用程序来进行很好的比较。

据我所知,在循环浏览集合数组并返回单个项目的承诺时,您可能需要使用

试试这样的

getCollection().then(function(collection){
  var arr =[];
  angular.forEach(collection,function (item){
    arr.push(getSingular(item.id));
  });
  return $q.all(arr);
}).then(function(t){
  // note this response will be an array of the returned promises.
  $scope.foreignParents = t;
});
我冒昧地为你创造了一个新的世界

我希望这对你有帮助

<ul>
  <li ng-repeat="p in foreignParents">
    <ul>
      <li ng-repeat="getChildren(p)">
        {{isImported(c) ? 'Imported' : 'Not Imported'}}
      </li>
    </ul>
  </li>
</ul>
app.controller('ImportCtrl', function ($scope, foreignParent, foreignChild, localChild) {

  $scope.foreignParents = [];

  $scope.githubRepositoryImported = function(repo) {
    localChild.RefNameExists(repo.ref_name);
  };

  foreignParent.Get().then(function(collection){
    _.each(collection.data, function(elem, i, list){
      foreignChild.Get(elem.login).then(function(collection){
        $scope.foreignParents[i].children = collection.data;
      });
    });
    $scope.foreignParents = collection.data;
  })

}]);
getCollection().then(function(collection){
  var arr =[];
  angular.forEach(collection,function (item){
    arr.push(getSingular(item.id));
  });
  return $q.all(arr);
}).then(function(t){
  // note this response will be an array of the returned promises.
  $scope.foreignParents = t;
});