AngularJS Tutorial - Pass Configuration Params to Filters








Angular filters can be passed a hash of params which can be directly accessed in the filter function.

Example

The following code shows how to pass Configuration Params to Filters.


<!doctype html>
<html  ng-app="MyApp">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
    <script>
var app = angular.module("MyApp", []);
<!-- w ww.  j av  a2 s  . co  m-->
app.filter("exclude", function() {
  return function(input, exclude) {
    var result = [];
    for (var i=0; i<input.length; i++) {
      if (input[i] !== exclude) {
        result.push(input[i]);
      }
    }

    return result;
  };
});    
    </script>
  </head>
  <body>
    <ul ng-init="names = ['P', 'A', 'B','C']">
      <li ng-repeat="name in names | exclude:'B'">
        <span>{{name}}</span>
      </li>
    </ul>
  </body>
</html>

The code above is rendered as follows: