Filters are used to format or transform data. Below is a list of common filters:
A filters can be added to an expression using the pipe character ( | ) and then followed by the filter name.
Open the module_demo.html file and change the HTML code to the following highlighted changes.
<script>
angular.module('myApp',[]).controller('myCtrl', function($scope){
$scope.firstName = "Cornelius",
$scope.lastName = "Chopin"
});
</script> <div ng-app="myApp" ng-controller="myCtrl">
<p>My first name is {{firstName | uppercase}} {{lastName | lowercase}}</p>
</div>
NOTE: Notice that the first name is in all uppercase and the last name in in all lowercase even though the first and last names were written as Cornelius Chopin.
Filter With Expression DemoA filters can be added to a directive using the pipe character (|) and then followed by the filter name.
<script>Filter With Directive Demo
angular.module('myApp',[]).controller('myCtrl', function($scope){
$scope.familyMembers = [
{firstName:'Cornelius',lastName:'Chopin'},
{firstName:'Sheila',lastName:'Chopin'},
{firstName:'Joshua',lastName:'Chopin'},
{firstName:'Josiah',lastName:'Chopin'}];
});
</script> <div ng-app="myApp" ng-controller="myCtrl">
<p>Order by first name:</p>
<ul>
<li ng-repeat="x in familyMembers | orderBy:'firstName'">
{{x.firstName + ' ' + x.lastName}}
</li>
</ul>
</div>
Here is an example of the currency filter.
<script>
var app = angular.module('myApp',[]);
app.controller('priceCtrl', function($scope) {
$scope.price = 88;
});
</script>
<div ng-app="myApp" ng-controller="priceCtrl">
<h1>Price: {{price | currency}}</h1>
</div>
NOTE: The result is $88.00. Notice that the filter added the dollar sign ($) on the front of the number AND a period and two zeros on the end of the number (.00).
Filter: Currency Filter DemoThe filter filter will, well, “filter” a subset of an array based on some criteria and return a result if a match is found:
<script>Filter: Filter Filter Demo
angular.module('myApp',[]).controller('filterCtrl',function($scope){
$scope.names = ['Cornelius','Sheila','Joshua','Josiah'];
});
</script> <p>Filter only names with the letters "Jos" in them.</p>
<div ng-app="myApp" ng-controller="filterCtrl">
<ul>
<li ng-repeat="x in names | filter : 'Jos'">{{x}}</li>
</ul>
</div>
Use can filter an array based on user input by setting the ng-model directive on an input field as an expression in a filter. Here, we’ll create a search field:
<script>Filter: Filter An Array Demo
angular.module('myApp',[]).controller('searchCtrl', function($scope){
$scope.names = ['Cornelius','Sheila','Joshua','Josiah'];
});
</script>
<div ng-app="myApp" ng-controller="searchCtrl">
<p>Type a search word:</p>
<p><input type="text" ng-model="searchword"></p>
<ul>
<li ng-repeat="x in names | filter:searchword">{{x}}</li>
</ul>
</div>
NOTE: Notice that a shorthand syntax was used for the script. However, both works the same—it's a preference on how you want to write it.
angular.module('myApp', []).controller('searchCtrl',function($scope){
$scope.names = ['Cornelius','Sheila','Joshua','Josiah'];
});
Instead of this:
var myApp = angular.module('myApp',[]);
myApp.controller('searchCtrl',function($scope){
$scope.names = ['Cornelius','Sheila','Joshua','Josiah'];
});
NOTE: Changes are highlighted in bold.
An array can be sorted by adding an ng-click directive to the table headers:
<script>Filter: Sort An Array Demo
angular.module('myApp',[]).controller('sortCtrl',function($scope){
$scope.cities = [
{city:'Austin',state:'Texas'},
{city:'Dallas',state:'Texas'},
{city:'Houston',state:'Texas'},
{city:'Baton Rouge',state:'Louisiana'},
{city:'New Orleans',state:'Louisiana'},
{city:'Lake Charles',state:'Louisiana'},
{city:'Miami',state:'Florida'},
{city:'Tampa Bay',state:'Florida'}];
$scope.orderByMe = function(x){
$scope.myOrderBy = x;
}
});
</script> <p>Click a table header to sort that column:</p>
<table ng-app="myApp" ng-controller="sortCtrl" border="1" width="50%" style="border-collapse:collapse">
<tr>
<th ng-click="orderByMe('city')">City</th>
<th ng-click="orderByMe('state')">State</th>
</tr>
<tr ng-repeat="x in cities | orderBy:myOrderBy">
<td>{{x.city}}</td>
<td>{{x.state}}</td>
</tr>
</table>
UPDATE TO CREATE MY OWN CUSTOM FUNCTION IN RED
You can create a custom filter by registering a new filter factory function. In the example below, the myFormat filter will format every other character to uppercase.
<script>Filter: Custom Filter Demo
var app = angular.module('myApp',[]); app.controller('myCtrl', function($scope) {
$scope.familyMembers = ['Cornelius','Sheila','Joshua','Josiah'];
});
app.filter('myCustomFilter', function(){
return function(x){
var i, c, txt = "";
for (i = 0; i < x.length; i++){
c = x[i];
if (i % 2 == 0){
c = c.toUpperCase();
}
txt += c;
}
return txt;
};
});
</script>
<ul ng-app="myApp" ng-controller="myCtrl">
<li ng-repeat="x in familyMembers">{{x | myCustomFilter}}</li>
</ul>