AngularJS filter based on array of strings?

后端 未结 1 690
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 00:32

I\'m having a hard time wrapping my head around how to go about doing an Angular filter to solve a problem that I\'m running into.

Here\'s a basic example of my data

相关标签:
1条回答
  • 2020-12-03 00:39

    You could write a custom filter. The filter would be given the list of active tags, tags, and the array of tasks to be filtered, tasks, and would output an array of filtered tags. It will be much the same as what you've already done, but with no extra function on the scope.

    angular.module('myApp').filter('selectedTags', function() {
        return function(tasks, tags) {
            return tasks.filter(function(task) {
    
                for (var i in task.Tags) {
                    if (tags.indexOf(task.Tags[i]) != -1) {
                        return true;
                    }
                }
                return false;
    
            });
        };
    });
    

    Then you can use it like

    <ul>
        <li ng-repeat="task in tasks | selectedTags:tags"></li>
    </ul>
    

    Check out this fiddle.

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