循环包含promises的数组

时间:2022-03-19 20:16:28

im building an angularjs application, were in $scope.list Arary items are stored, that are created by a specific ngResource factory. I delete some items on the fly by simply slicing them from the list.

我建立一个angularjs应用程序,在$ scope.list中存储了由特定ngResource工厂创建的存档项目。我只需从列表中切片就可以删除一些物品。

$scope.list.splice(pos,1);

Later in code I iterate over existing elements and want to save them with :

稍后在代码中迭代现有元素并希望将它们保存为:

for (var elem in $scope.list ){
     $scope.list[elem].$save();
 }

The Problem is that, if the list is empty i have an $promise in the $scope.list How should i process, so that the save is not called for the element that just have an promise?

问题是,如果列表为空,我在$ scope.list中有一个$ promise我应该如何处理,以便不为具有承诺的元素调用save?

1 个解决方案

#1


1  

You should start by filtering your array and then save the items in the filtered array:

您应该首先过滤数组,然后将项目保存在已过滤的数组中:

$scope.list
.filter(function (elem) {
  return typeof elem.then !== 'function';
})
.forEach(function (elem) {
  elem.$save();
});

#1


1  

You should start by filtering your array and then save the items in the filtered array:

您应该首先过滤数组,然后将项目保存在已过滤的数组中:

$scope.list
.filter(function (elem) {
  return typeof elem.then !== 'function';
})
.forEach(function (elem) {
  elem.$save();
});