AngularJS_Basics6_Filters

<!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="namesCtrl"> <p>The name is {{ lastName | uppercase }}</p> <p>The name is {{ lastName | lowercase }}</p> <p>Looping with objects:</p> <ul> <li ng-repeat="x in names | orderBy:'country' | filter : 'Gl'"> {{ x.name + ', ' + x.country }} </li> </ul> <p>Type a letter in the input field:</p> <p><input type="text" ng-model="test"></p> <ul> <li ng-repeat="x in names | filter:test"> {{ x }} </li> </ul> <p>Click the table headers to change the sorting order:</p> <table border="1" width="100%"> <tr> <th ng-click="orderByMe('name')">Name</th> <th ng-click="orderByMe('country')">Country</th> </tr> <tr ng-repeat="x in names | orderBy:myOrderBy"> <td>{{x.name}}</td> <td>{{x.country}}</td> </tr> </table> <h1>Price: {{ price | currency : "$" }}</h1> <li ng-repeat="x in names2"> {{x | myFormat}} </li> </div> <script> var app = angular.module('myApp', []).controller('namesCtrl', function($scope) { $scope.firstName = "Priya", $scope.lastName = "Mounica", $scope.names = [ {name:'Jani',country:'Norway'}, {name:'Carl',country:'Sweden'}, {name:'Margareth',country:'England'}, {name:'Hege',country:'Norway'}, {name:'Joe',country:'Denmark'}, {name:'Glustav',country:'Sweden'}, {name:'Birgit',country:'Denmark'}, {name:'Mary',country:'England'}, {name:'Kai',country:'Norway'} ]; $scope.price = 58; $scope.names2 = [ 'Jani', 'Carl', 'Margareth', 'Hege', 'Joe', 'Gustav', 'Birgit', 'Mary', 'Kai' ]; $scope.orderByMe = function(x) { $scope.myOrderBy = x; } }); app.filter('myFormat', 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> </body> </html>
AngularJS provides filters to transform data:
--> currency : Format a number to a currency format.
--> date : Format a date to a specified format.
--> filter : Select a subset of items from an array.
--> json : Format an object to a JSON string.
--> limit : To Limits an array/string, into a specified number of elements/characters.
--> lowercase : Format a string to lower case.
--> number : Format a number to a string.
--> orderBy : Orders an array by an expression.
--> uppercase : Format a string to upper case.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.