Create custom Filter

Description

To create a filter, we put it under its own module.

Example

The following code creates a filter that capitalizes the first character of a string.


<!DOCTYPE html>
<html  ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<!--  w w  w  .j a va 2s.c o  m-->
</head>
<body ng-app>
   <h2>{{ 'this is a test' | capitalize }}</h2>
  <script>
    angular.module('myApp', ['myApp.filters']);

    angular.module('myApp.filters', [])
    .filter('capitalize', function() {
      return function(input) {
        // input will be the string we pass in
        if (input) {
          return input[0].toUpperCase() + input.slice(1);
        }
      }
    });
  </script>
     
</body>
</html>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Tutorial »




Introduction
Buildin Filters
Buildin Directives