Pass Configuration Params to Filters

Description

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.


<!-- w ww  . ja v a 2 s .  c  o m-->



<!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", []);

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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities