Using Filters With Directives in AngularJS

前端 未结 2 1953
礼貌的吻别
礼貌的吻别 2020-12-19 08:30

I\'m attempting to use filters within an AngularJS directive but not sure exactly how to do so. From some info on the mailing list it appears that you should be able to inj

相关标签:
2条回答
  • 2020-12-19 08:35

    You're creating a new isolate scope on the directive (scope: { 'fancyDisplay': '=' }), that means you won't be able to access properties from the parent scope. Since tagFilter is defined on the parent scope, you won't be able to access it.

    Pass tagFilter as an attribute to the directive:

    <div fancy-display="model.data" filter="tagFilter"></div>
    

    And on the directive:

    scope: {
        fancyDisplay: '=',
        tagFilter: '=filter'
    },
    

    jsfiddle: http://jsfiddle.net/bmleite/VDLqa/5/

    0 讨论(0)
  • 2020-12-19 08:47

    Thanks to @bmleite for your answer.

    Another thing that might be helpful is to make sure to declare your ng-repeat directive like this incase you have deplicates on your list.

    It took me forever to figure this out. Apparently you must filter before you specify the track by x :

    app.directive("someDirective", function(){ ...
    
        restrict: "E",
        ...
        template:"<ul ng-repeat='t in tree | filter:key track by $index'><li>{{t}}</li></ul>",
    
    });
    

    The inverse does not work.

    0 讨论(0)
提交回复
热议问题