AngularJS filter

AngularJS custom 'reverse' filter example:

html:

<section ng-app="app" class="container">
	<section ng-controller="appController">
		<h3>List of countries with reversed capitals using filter</h3>
		<ul>
			<li ng-repeat="country in countriesList">
                {{country.name}} - {{country.capital | reverse}}
			</li>
		</ul>
	</section>
</section>

js:

var app = angular.module('app', []);
 
app.controller('appController', function ($scope) {
 
	$scope.countriesList = [
		{name: 'Germany', capital: 'Berlin', population: '81,305,856'},
		{name: 'France', capital: 'Paris', population: '65,630,692'},
		{name: 'Italy', capital: 'Rome', population: '61,261,254'},
		{name: 'Spain', capital: 'Madrid', population: '47,042,984'},
		{name: 'United Kingdom', capital: 'London', population: '63,047,162'}
	];
 
});
 
app.filter('reverse', function(){
    return function(text){
        return text.split('').reverse().join('');
    }
});

Leave a Reply

Your email address will not be published. Required fields are marked *