Rajkamal Subramanian
Rajkamal Subramanian

Reputation: 6944

how to pass param to a filter function in angular js

I have two questions.

  1. how to pass param to a filter function.

    say for example: item in masterData|filter1:masterdata|filter2:outputFromfilter1, myparam | filter3:outputFromfilter2, myparam1,myparam2

  2. how to access the controller $scope inside the filter function.

    animateAppModule.filter( 'distinct' , function(){
        return function(masterdata){
            //HOW TO ACCESS THE $scope HERE
        }
    })
    

Here is a fiddle. Pls. look in to the firebug console, to see that the parameters passed to the filter is undefined.

Upvotes: 10

Views: 13849

Answers (1)

Tosh
Tosh

Reputation: 36030

For your 1st question:

You can give parameters separated by : into the filter. For example,

{{ array | myfilter:a:b:c }}

In your filter definition,

angular.module('app', []).
  filter('myfilter', function() {
    return function(in, param1, param2, param3) {
      // do something
    };
  });

for your 2nd question.

Not sure why you need to access $scope. Can you simply feed whatever needed information via param as your Q1?

Upvotes: 30

Related Questions