Angular.js pass filter to directive bi-directional ('=') attribute

后端 未结 2 649
情歌与酒
情歌与酒 2020-12-17 15:11

I need to use sublist directive in few places of the page, and it should contain sometimes full fields list, but sometimes filtered. Here is my nai

相关标签:
2条回答
  • 2020-12-17 15:44

    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.

    Here is a fiddle

    0 讨论(0)
  • 2020-12-17 15:47

    Corrected Fiddle

    Check a related post here.

    In the fiddle you will need to have closing tags. While you can still have self contained tags like the one you have.

     <sublist fields="fields" filter="'Rumba'"/> <!-- Tested in chrome -->
    
    0 讨论(0)
提交回复
热议问题