shm*_*uli 18 angularjs angularjs-digest
我已经为AngularJS编写了这个自定义过滤器,但是当它运行时,我得到了无限的摘要循环错误.为什么会发生这种情况,我该如何纠正?
angular.module("app", []).
filter('department', function(filterFilter) {
return function(items, args) {
var productMatches;
var output = [];
var count = 0;
if (args.selectedDepartment.Id !== undefined && args.option) {
for (let i = 0; i < items.length; i++) {
productMatches = items[i].products.filter(function(el) {
return el.Order__r.Department__r.Id === args.selectedDepartment.Id;
});
if (productMatches.length !== 0) {
output[count] = {};
output[count].products = productMatches;
output[count].firstProduct = items[i].firstProduct;
count++;
}
}
}
return output;
};
}).
Run Code Online (Sandbox Code Playgroud)
这是相关的HTML:
<tr class='destination' ng-repeat-start='pickupAccount in pickupAccounts | department : {"selectedDepartment": selectedDepartment, "option": displayExclusive }'>
<!-- td here -->
</tr>
Run Code Online (Sandbox Code Playgroud)
displayExclusive 是布尔值.
Max*_*tin 10
我已经为AngularJS编写了这个自定义过滤器,但是当它运行时,我得到了无限的摘要循环错误.
请记住,过滤器应该返回相同对象结构的数组.当我们激活过滤器时,它会触发将再次在我们的过滤器上运行的摘要循环.如果输出列表中的内容发生了变化 - 触发新的摘要周期等等.经过10次尝试后,它会抛出Infinite Digest Loop异常
这个空的过滤器将起作用(100%).实际上我们在这里什么都不做,但返回过滤器收到的同一个对象.
filter('department', function(filterFilter) {
return function(items, args) {
var output = items;
return output;
};
})
Run Code Online (Sandbox Code Playgroud)
现在的主要思想是:根据一些陈述,写一些条件output从输入列表ae 推送到对象itemsif
var output = [];
if (args.selectedDepartment.Id !== undefined && args.option) {
angular.forEach(items, function(item) {
if(<SOME CONDITION>) {
output.push(item);
}
});
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式它也会起作用.
我们有这样的逻辑:
productMatches = items[i].products.filter(function(el) {
return el.Order__r.Department__r.Id === args.selectedDepartment.Id;
});
if (productMatches.length !== 0) {
output[count] = {};
output[count].products = productMatches;
output[count].firstProduct = items[i].firstProduct;
count++;
}
Run Code Online (Sandbox Code Playgroud)
这里我们完全修改了存储的对象output.所以下一个消化周期我们items会一次又一次地改变.
主要目的filter是过滤列表而不是修改列表对象内容.
您编写的上述逻辑与数据操作有关,而与过滤无关.该department过滤器返回的项目相同的长度.
为了实现您的目标,您可以使用lodash map或underscorejs map.
| 归档时间: |
|
| 查看次数: |
619 次 |
| 最近记录: |