Home > Article > Web Front-end > AngularJS Basic Knowledge Notes - Filter_AngularJS
Filters are used to modify data and can be included in expressions or using pipe directives. Below is a list of commonly used filters.
S.No. Name Description
1 Uppercase Convert text to uppercase text.
2 Lowercase Convert text to lowercase text.
3 Currency Currency format text.
4 Filter Filters a subset of the array based on the provided criteria.
5 Sorting Sorting provides standard basic arrays. Caps filter
Add uppercase filter expression using pipe character. Here, a caps filter has been added to print student names in all caps.
Enter first name:<input type="text" ng-model="student.firstName"> Enter last name: <input type="text" ng-model="student.lastName"> Name in Upper Case: {{student.fullName() | uppercase}}
Lower case filter
Add lowercase filter, using pipe expression. Add lowercase filter here to print student names in lowercase letters.
Enter first name:<input type="text" ng-model="student.firstName"> Enter last name: <input type="text" ng-model="student.lastName"> Name in Upper Case: {{student.fullName() | lowercase}}
Currency Filter
The Canadian dollar filter uses the pipe character to return a number expression. Here we have added the filter Currency to print the charges using Currency format.
Enter fees: <input type="text" ng-model="student.fees"> fees: {{student.fees | currency}}
Filter of filters
To show only the required subjects we use subjectName as filter.
Enter subject: <input type="text" ng-model="subjectName"> Subject: <ul> <li ng-repeat="subject in student.subjects | filter: subjectName"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul>
Sort Filter
To order topics by tag, we use orderBy tag.
Subject:
<ul> <li ng-repeat="subject in student.subjects | orderBy:'marks'"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul>
Example
The following example will demonstrate all the above filters.
testAngularJS.html
<html> <head> <title>Angular JS Filters</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app="" ng-controller="studentController"> <table border="0"> <tr><td>Enter first name:</td><td><input type="text" ng-model="student.firstName"></td></tr> <tr><td>Enter last name: </td><td><input type="text" ng-model="student.lastName"></td></tr> <tr><td>Enter fees: </td><td><input type="text" ng-model="student.fees"></td></tr> <tr><td>Enter subject: </td><td><input type="text" ng-model="subjectName"></td></tr> </table> <br/> <table border="0"> <tr><td>Name in Upper Case: </td><td>{{student.fullName() | uppercase}}</td></tr> <tr><td>Name in Lower Case: </td><td>{{student.fullName() | lowercase}}</td></tr> <tr><td>fees: </td><td>{{student.fees | currency}}</td></tr> <tr><td>Subject:</td><td> <ul> <li ng-repeat="subject in student.subjects | filter: subjectName |orderBy:'marks'"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul> </td></tr> </table> </div> <script> function studentController($scope) { $scope.student = { firstName: "Mahesh", lastName: "Parashar", fees:500, subjects:[ {name:'Physics',marks:70}, {name:'Chemistry',marks:80}, {name:'Math',marks:65} ], fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + " " + studentObject.lastName; } }; } </script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> </body> </html>
The above is the entire content of this article, I hope you all like it.