AngularJS Tutorial - Custom filter to check input value








The following code shows how to create Custom filter to check input value.

Example


<!doctype html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>
</head><!--from  w w  w .ja va 2  s.  c om-->
<body>
  <div>
    <h2>{{ 1 | messageIfNegative }}</h2>
    <h2>{{ -1 | messageIfNegative }}</h2>
  </div>

  <script>
    angular.module('myApp', ['myApp.filters']);

    angular.module('myApp.filters', [])
    .filter('messageIfNegative', function() {
      return function(input) {
        if (input <= 0) return 'less than zero';
        else return input;
      }
    });
  </script>

</body>
</html>

The code above is rendered as follows: