Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass param to a filter function in angular js

Tags:

angularjs

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.

like image 453
Rajkamal Subramanian Avatar asked Feb 12 '13 21:02

Rajkamal Subramanian


People also ask

How do you indicate a parameter to a filter in AngularJS?

Using the filter filter you won't be able to pass in a parameter but there are at least two things you can do. 1) Set the data you want to filter by in a scope variable and reference that in your filter function like this fiddle. 2) Create a new filter that takes in a parameter like this fiddle.

What is the correct way to apply multiple filter in AngularJS?

7) Which of the following syntax is correct for applying multiple filters in AngularJS? Answer: A is the correct option. The syntax of applying multiple filters in AngularJS can be written as: {{ expression | filter1 | filter2 | ... }}

What is filter function in AngularJS?

The “filter” Filter in AngularJS is used to filter the array and object elements and return the filtered items. In other words, this filter selects a subset (a smaller array containing elements that meet the filter criteria) of an array from the original array.

What is custom filter in AngularJS?

Introduction to AngularJS Custom Filter. In AngularJS filters are used to modify or update the data before rendering the data on view or UI. Filters are clubbed in expression or directives using pipe (|) symbol.


1 Answers

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?

like image 99
Tosh Avatar answered Sep 30 '22 16:09

Tosh