简体   繁体   English

与ng-repeat一起使用功能会导致无限摘要循环

[英]Using function with ng-repeat causing infinite digest loop

I have the following piece of code. 我有以下代码。

https://plnkr.co/edit/Tt7sWW06GG08tdJu72Fg?p=preview . https://plnkr.co/edit/Tt7sWW06GG08tdJu72Fg?p=preview Please expand the window fully to see the whole view. 请完全展开窗口以查看整个视图。

I have categories and sub-categories to display in a navbar as shown in the above plunkr. 我在导航栏中显示了categoriessub-categories ,如上面的plunkr所示。 Each category has services/sub-categories to display. 每个类别都有要显示的服务/子类别。

I am using 2 nested ng-repeats and the inner one is using the value from the parent iteration of ng-repeat . 我正在使用2个嵌套的ng-repeats ,而内部的正在使用ng-repeat父迭代的值。 I have attached a function getServicesByCategory to the inner ng-repeat that calls getServicesByCategory from the DocumentsFactory and resolves the promise to return data in an array object subcategories , which eventually should get iterated by inner ng-repeat . 我已将函数getServicesByCategory附加到内部ng-repeat ,该函数从DocumentsFactory调用getServicesByCategory并解决了返回数组对象subcategories中的数据的promise ,最终应通过内部ng-repeat迭代。

The categories are displaying fine but the sub-categories are not. 类别显示良好,但子类别却没有。

There are 2 problems with this. 这有两个问题。

1) . 1) The subcategories are being shown the same for every category, and the ones being shown belong to the last category in the outer ng-repeat iteration ie Utility Services . 每个类别的子类别都显示为相同,而所显示的类别属于外部ng-repeat迭代中的最后一个类别,即Utility Services Instead each category should shows it's own sub-categories. 相反,每个类别应显示其自己的子类别。

2) . 2) The console shows multiple errors of infinite digest cycle aborting due to attempts exceeding than 10 with these errors http://errors.angularjs.org/1.5.6/ $rootScope/infdig?p0=10&p1=%5B%5D repeatedly in console. 控制台在控制台中多次显示由于尝试超过10次而导致infinite digest cycle中止的错误,这些错误http://errors.angularjs.org/1.5.6/ $ rootScope / infdig?p0 = 10&p1 =%5B%5D反复出现。 The method getServicesByCategory is being called in an infinite loop triggering multiple digest cycles. 在无限循环中调用方法getServicesByCategory ,触发多个摘要循环。

Seems like a simple category problem complicated by Angular's dirty checking and digest cycles . 似乎是一个简单的类别问题,由于Angular的dirty checkingdigest cycles而变得复杂。

I read multiple threads and posts suggesting to not return a new object inside the function associated with ng-repeat which I am not. 我读了多个线程和帖子,建议不要在与ng-repeat相关的函数中不返回新对象,而我不是。 I am returning the reference of subcategories . 我将返回subcategories的参考。

The inner ng-repeat is dependent upon the data from outer one. inner ng-repeat依赖于outer ng-repeat。 Binding a function to ng-repeat was only option for me. 将功能绑定到ng-repeat是我的唯一选择。 Is this the right approach? 这是正确的方法吗? The API's to fetch categories and subcategories are different and I dont have the flexibility to change them. 用于获取类别和子类别的API是不同的,我没有灵活性来更改它们。 I have been unable to come up with a solution. 我一直无法提出解决方案。 Just want to right sub-categories against each category. 只是想针对每个类别对子类别进行排序。

Any help will be highly appreciated! 任何帮助将不胜感激!

Note : Open the view in full window to see the navbar on the left. 注意 :在全窗口中打开视图以查看左侧的导航栏。

The solution here is to get all the data you need and create an array of categories, each one containing its own sub categories. 这里的解决方案是获取所需的所有数据并创建一个类别数组,每个类别都包含自己的子类别。 And use this object in the nested ng-repeats . 并在嵌套的ng-repeats使用此对象。

Please check my updated version of your plunker: 请检查您的插件的更新版本:

https://plnkr.co/edit/BaHOcI4Qa8t5CkbshyCX https://plnkr.co/edit/BaHOcI4Qa8t5CkbshyCX

As you can see, I use: 如您所见,我使用:

ng-repeat="category in documents.categories"

In the outer ng-repeat and: 在外部ng-repeat和:

ng-repeat="service in category.subCategories"

In the inner one. 在内部。

In the Controller I build the category.subCategories in the following way: 在Controller中,我通过以下方式构建category.subCategories

documentsFactory
  .getCategories() // Get categories
  .then(function(response) { // Extract them from the response
    return response.data.subCategory;
  })
  .then(getSubcategories) // For every catgory, get its own subcategory
  .then(function(categories) { // Add categories to the $scope
    self.categories = categories;
  });

Where getSubcategories is: 其中getSubcategories是:

function getSubcategories(categories) {
  console.log(categories)
  var retrievSubcategories = categories.map(function(category) { // Create a promise to get the subcategories for every category
    return documentsFactory
      .getServicesByCategory(category.name) // Get the subcategories for the given category
      .then(function(response) {
        category.subCategories = response.data.documents; // Add subgategories to the category
        return category;
      });
  });
  return $q.all(retrievSubcategories) // Return a promise resolve only when all the subcategories for every category are retrieved
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM