Introduction
This is the next article of my Angular series, continuing from Angular js from basic to expert -day five. In this article, I am going to explain custom filters in AngularJS and how to create a custom filter. Please find the previous article on built-in filters here.
AngularJS also provides the functionality of creating our own custom filter. We can create our own custom filters by registering the new filter factory function with our module. Please check the previous article for built-in filter in AngularJS here.
$filterProvider is responsible to create a filter function. $filterProvider is factory function and AngularJS internally use $filterProvider to create a filter function.
Creating custom filters is very easy. We only need to register a new filter factory method with our module. As we create the controller using angApp.controller(), the same way, we can create filter using angApp.filter().
- var angApp = angular.module('MyERPDemoApp', []);
- angApp.filter('myNewFormat', function(data) { return data});
AngularJS provides filter methods to create the custom filter. Internally, AngularJS uses the $filterProvider. This factory function returns a new filter function which takes an input value as the first parameter. Any other filter parameters are passed in as an additional parameter into the filter function.
Syntax
- var angApp = angular.module('MyERPDemoApp', []);
- angApp.filter('myNewFormat', function() {
- return function(datatofilter) {
-
- return datatofilter;
- };
- });
- HTML:
- {{somedata | myNewFormat }}
Let's create a custom filter to convert the first letter of the word in upper case. For example, if my name string is written like ‘arvind singh baghel’, it should be Arvind Singh Baghel. (Check comments in the code for details).
- var angApp = angular.module("MyERPDemoApp", []);
-
-
-
- angApp.filter('myNewFormat', function()
- return function(datatofilter) {
- var res = datatofilter.split(" ");
-
-
-
- for(var i=0; i < res.length;i++){
- res[i]= res[i].substr(0,1).toUpperCase()+res[i].substr(1);
- }
-
- var fullstring= res.join(" ");
- return fullstring;
- };
- });
In the below example, I will cover all the built-in filters and create a custom filter which will format the first character of every word in upper case. The following details are explained using the comments inside the code.
- <!DOCTYPE html>
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js">
- </script>
- <script>
- var angApp = angular.module("MyERPDemoApp", []);
-
- // creating custom filter to convert first letter of word in upper case
- // e.g input arvind singh baghel, output: Arvind Singh Baghel
- // creating custome filter using filter function named myNewFormat
- angApp.filter('myNewFormat', function() {
- return function(datatofilter) {
- var res = datatofilter.split(" "); // split input by space in array
- //
- for(var i=0; i<res.length;i++){
- res[i]= res[i].substr(0,1).toUpperCase()+res[i].substr(1);
- }
- //
- return res.join(" ");
- };
- });
-
- //Creating controller with name UserController.
- angApp.controller('UserController', function($scope) {
- $scope.welcomenote="Welcome to Angular JS world";
- $scope.title="Angular JS filter example";
- $scope.PersonNames = [
- {name:'arvind singh baghel',DOB:new Date(),country:'India', fee: 100},
- {name:'tom cruis',DOB:new Date(),country:'united States',fee:null} ,
- {name:'denial crg',DOB:new Date(),country:'United kingdom',fee:null},
- {name:'ronaldo',DOB:new Date(),country:'brazil',fee:1000} ,
- ];
-
- $scope.orderByThis = function(data) {
- $scope.customOrderBy = data;
- }
- });
-
- </script>
- </head>
- <body ng-app="MyERPDemoApp" ng-controller="UserController">
-
-
- Uppercase Filter : <span ng-bind="welcomenote | uppercase"> </span></br>
-
- lowercase filter : <span> {{ title | lowercase}} </span></br>
-
- <p>Enter Name To Filter : <input type="text" ng-model="name"><p>
- <table border="1" >
- <tr > <td ng-click="orderByThis('name')">Name</td> <td ng-click="orderByThis('DOB')">DOB</td> <td ng-click="orderByThis('country')">Country</td> <td ng-click="orderByThis('fee')">Fee</td>
- </tr>
-
- <tr ng-repeat="person in PersonNames | filter:name | orderBy:customOrderBy">
-
- <td>{{person.name | myNewFormat}}</td>
-
- <td>{{person.DOB | date:'dd/MM/yyyy'}}</td>
- <td>{{person.country}}</td>
-
- <td>{{person.fee | currency}}</td>
- </tr>
- </table>
- </body>
- </html>
Run this above sample and see the output.