先上代码.
1 <!DOCTYPE html> 2 <html ng-app="FilterModule"> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 <script src="js/angularJs-1.2.16-min.js"></script> 7 </head> 8 <body ng-controller="HomeController"> 9 <input type="text" ng-model="num" /> 10 过滤结果:{{num|number:2}}<br/> 11 <h1>{{someWord | ChangeFirstWord}}</h1> 12 13 </body> 14 <script> 15 var homeModule=angular.module("FilterModule",[]); 16 homeModule.filter("ChangeFirstWord", function () { 17 var titleCaseFilter= function (zfc) { 18 var word=""; 19 for(var i=0;i<zfc.length;i++){ 20 if(i==0){ 21 word+=zfc[i].toUpperCase(); 22 } 23 else{ 24 word+=zfc[i]; 25 } 26 } 27 return word; 28 }; 29 return titleCaseFilter; 30 }); 31 homeModule.controller("HomeController", function ($scope) { 32 $scope.someWord="my heater will go on"; 33 }); 34 /*function HomeController($scope){ 35 $scope.pageHeading="guds oyansi"; 36 }*/ 37 38 </script> 39 </html>
11行中的number过滤器是angular自带的.可以将数字按照自己预定的格式进行过滤.
后面的:2表示保留的小数位数.
angular自定义过滤器是非常方便的.上面的ChangeFirstWord就是我们自己定义的一个过滤器,
过滤将字符串中的首字母变成大写的格式.
So easy!