且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular.js通滤波器,以指令双向(“=”)属性

更新时间:2021-11-21 20:57:17

$消化迭代的错误通常发生在有改变模型中的守望者。在错误的情况下,分离字段被绑定到一个过滤器的结果。该绑定创建一个观察者。由于过滤器返回从每次运行时函数调用一个新的对象,它会导致观察者不断触发,因为旧的价值永远不匹配的新的(见的从伊戈尔在谷歌群组这个评论)

The $digest iterations error typically happens when there is a watcher that changes the model. In the error case, the isolate fields binding is bound to the result of a filter. That binding creates a watcher. Since the filter returns a new object from a function invocation each time it runs, it causes the watcher to continually trigger, because the old value never matches the new (See this comment from Igor in Google Groups).

一个很好的解决将是绑定字段在这两种情况下,如:

A good fix would be to bind fields in both cases like:

<sublist fields="fields" /></sublist>

和添加另一个可选属性的第二种情况下过滤:

And add another optional attribute to the second case for filtering:

<sublist fields="fields" filter-by="'Rumba'" /></sublist>

然后调整指令,如:

Then adjust your directive like:

return {
    restrict: 'E',
    scope: {
        fields: '=',
        filterBy: '='
    },
    template: '<div ng-repeat="f in fields | filter:filterBy">'+
              '<small>here i am:</small> {{f}}</div>'
};

请注意:请记住关闭子列表标签在你的提琴

Note: Remember to close your sublist tags in your fiddle.

这里是一个小提琴