Javascript 角度-已达到10$digest()迭代次数。创建数组的深度副本时中止
我想在表格标题中创建带有过滤器的表格。筛选器是每列中唯一值的列表。以下是简化代码:Javascript 角度-已达到10$digest()迭代次数。创建数组的深度副本时中止,javascript,jquery,angularjs,arrays,Javascript,Jquery,Angularjs,Arrays,我想在表格标题中创建带有过滤器的表格。筛选器是每列中唯一值的列表。以下是简化代码: <table> <tr> <th class="dropdown" ng-repeat="field in data.columns"> <span>{{field.title}}</span> <ul class="dropdown-menu"> <li ng
<table>
<tr>
<th class="dropdown" ng-repeat="field in data.columns">
<span>{{field.title}}</span>
<ul class="dropdown-menu">
<li ng-repeat="item in unique(data.items, field)"><checkbox> {{item.text}}</li>
</ul>
</th>
</tr>
<tr ng-repeat="item in data.items"></tr>
</table>
问题出现在属性something array.slice0不拷贝和$.extend copy中,但角度获取错误
感谢您的建议您尚未发布过滤器的代码,因此我必须假设发生了什么。我认为在制作副本之前,您并没有检查列表中的所有元素是否都是唯一的。这会导致过滤器在每个摘要中更改数据,从而触发无限多个摘要-因此产生错误。你的名单永远不会稳定
要在过滤器开始时解决此问题,请添加检查元素的唯一性,并检查它们是否已经唯一(例如,在第一次通过过滤器后),只返回输入对象。这样模型就会稳定。以下是我从对象数组中获取唯一值的代码:
function toUnique(a, property, innerProperty) {
var lastIndex = a.length;
if (lastIndex === 0 || lastIndex === undefined)
return a;
var copyarr = jQuery.extend(true, [], a);
if (property !== undefined) {
while (prevIndex = --lastIndex) {
while (prevIndex--) {
var obj1 = copyarr[lastIndex];
var obj2 = copyarr[prevIndex];
if (obj1 !== undefined) {
if (copyarr[lastIndex][property] instanceof Array && copyarr[prevIndex][property] instanceof Array)
unique(copyarr[lastIndex][property], copyarr[prevIndex][property], innerProperty);
else
obj1[property] !== obj2[property] || copyarr.splice(prevIndex, 1);
}
}
}
return copyarr;
} else {
while (prevIndex = --lastIndex)
while (prevIndex--)
copyarr[lastIndex] !== copyarr[prevIndex] || copyarr.splice(prevIndex, 1);
return copyarr;
}
}
function unique(array1, array2, innerProperty) {
for (var i = 0; i < array1.length; i++) {
removeDuplicates(array1, i + 1, array1[i], innerProperty);
removeDuplicates(array2, 0, array1[i], innerProperty);
}
for (var i = 0; i < array2.length; i++) {
removeDuplicates(array2, i + 1, array2[i], innerProperty);
}
}
function removeDuplicates(arr, startPos, p, property) {
for (var i = startPos; i < arr.length;) {
if (p[property] == arr[i][property]) {
arr.splice(i, 1);
} else {
i++;
}
}
}
角度。复制角度的方式。我认为如果你将一些操纵数组的逻辑移到控制器上,你的问题就会消失。尝试将对uniquedata.items字段的调用移动到控制器函数。如果您可以减少其中一个ng重复,这将显著减少摘要周期的数量。另一种角度方法可以是使用$filter'filter'或为ng-repeat创建自定义筛选器。在检查lastIndex==0 | | lastIndex==undefined后,还可以检查所有元素是否已经唯一,如果是,则返回a。您也可以考虑使用LIQ.JS或LADASH之类的库,其中已经实现了唯一的检查。
function toUnique(a, property, innerProperty) {
var lastIndex = a.length;
if (lastIndex === 0 || lastIndex === undefined)
return a;
var copyarr = jQuery.extend(true, [], a);
if (property !== undefined) {
while (prevIndex = --lastIndex) {
while (prevIndex--) {
var obj1 = copyarr[lastIndex];
var obj2 = copyarr[prevIndex];
if (obj1 !== undefined) {
if (copyarr[lastIndex][property] instanceof Array && copyarr[prevIndex][property] instanceof Array)
unique(copyarr[lastIndex][property], copyarr[prevIndex][property], innerProperty);
else
obj1[property] !== obj2[property] || copyarr.splice(prevIndex, 1);
}
}
}
return copyarr;
} else {
while (prevIndex = --lastIndex)
while (prevIndex--)
copyarr[lastIndex] !== copyarr[prevIndex] || copyarr.splice(prevIndex, 1);
return copyarr;
}
}
function unique(array1, array2, innerProperty) {
for (var i = 0; i < array1.length; i++) {
removeDuplicates(array1, i + 1, array1[i], innerProperty);
removeDuplicates(array2, 0, array1[i], innerProperty);
}
for (var i = 0; i < array2.length; i++) {
removeDuplicates(array2, i + 1, array2[i], innerProperty);
}
}
function removeDuplicates(arr, startPos, p, property) {
for (var i = startPos; i < arr.length;) {
if (p[property] == arr[i][property]) {
arr.splice(i, 1);
} else {
i++;
}
}
}
var a = [{id: 1, title: "AAAA"}, {id: 2, title: "BBBB"}, {id: 1, title: "AAAA"}]
unique(a, "title", null); //return {id: 1, title: "AAAA"}, {id: 2, title: "BBBB"}
var b = [{id: 1, title: "AAAA", authors: [{id: 15, name: "John"}, {id: 25, name: "Peter"}, {id: 16, name: "John"}]},
{id: 1, title: "BBBB", authors: [{id: 15, name: "John"}, {id: 25, name: "Peter}]}]
unique(b, "authors", "name") //return: [{id: 1, title: "AAAA", authors: [{id: 15, name: "John"}, {id: 25, name: "Peter}]},
{id: 1, title: "BBBB", authors: []}]