How to pass multiple parameter in angular filter f

2019-03-27 04:20发布

I tried hard and visit lot of similar question like this but still unable to solve this issue.

I want to pass extra parameter in angular filter function. I found solution as below but it's not working. I am getting undefined for object which I have used in ng-repeat.

<li ng-repeat="user in users | filter:isStatus(user,secondParam)">{{user.name}}</li>

There are solution for angular custom filter as below but that also not working with angular filter function.

<li ng-repeat="user in users | filter:isStatus:user:secondParam">{{user.name}}</li>

jsFiddle - You can see my problem here.

4条回答
爷的心禁止访问
2楼-- · 2019-03-27 04:46

According to your case, you can use predicate expression instead of custom filter:

<li ng-repeat="user in users | filter:{status: status, name: name}">{{user.name}}</li>

Take a look at this fiddle: http://jsfiddle.net/ovym2tpr/28/

You can use custom filter in anyway, it just performs not very well especially under nested ng-repeat

查看更多
贪生不怕死
3楼-- · 2019-03-27 04:52

How do I call an Angular.js filter with multiple arguments?

AngularJS : Custom filters and ng-repeat

 myApp.filter("isStatus ", function() { // register new filter
     return function(user, secondParam, thirdParam) { // filter arguments

       return user.status == $scope.status; // implementation
    };
 });

Calling from Template

<li ng-repeat="user in users | isStatus:secondParam">{{user.name}}</li>
查看更多
我想做一个坏孩纸
4楼-- · 2019-03-27 04:55

Will try:

$scope.isStatus = function(secondParam, thirdParam){
      return function(user) {
           console.log(secondParam);
           console.log(thirdParam);
           return user.status == $scope.status;
     }

Updated version http://jsfiddle.net/4PYZa/282/

查看更多
该账号已被封号
5楼-- · 2019-03-27 05:02

It's very simple , just do this

<li ng-repeat="user in users | filter:user | filter : secondParam)">{{user.name}}</li>
查看更多
登录 后发表回答