实例解析angularjs的filter过滤器(3)

<!DOCTYPE html> <html lang="zh-CN" ng-app="nickApp"> <head> <meta charset="UTF-8"> <title>ng filter nick</title> </head> <body ng-controller="nickCtrl"> <ul> <li>!!1 求和</li> <li ng-repeat="item1 in items1"> <div ng-bind="item1.male | sumNick:item1.female:item1.gay"></div> </li> </ul> <ul ng-repeat="item1 in items1"> <li>!!2 求百分比</li> <li> <b>male</b> <i ng-bind="item1.male|percentNick:item1.female:item1.gay"></i> </li> <li> <b>female</b> <u ng-bind="item1.female|percentNick:item1.male:item1.gay"></u> </li> <li> <b>gay</b> <s ng-bind="item1.gay|percentNick:item1.female:item1.male"></s> </li> </ul> <ul> <li>3 按年龄排序(默认升序)</li> <li ng-repeat="item2 in items2|orderBy:'age'"> <div> <b>name</b> <u ng-bind="item2.name"></u> </div> <div> <b>age</b> <i ng-bind="item2.age"></i> </div> <div> <b>stature</b> <i ng-bind="item2.stature"></i> </div> </li> </ul> <ul> <li>4 按年龄排序(加参数true则为倒序即降序)</li> <li ng-repeat="item2 in items2|orderBy:'age':true"> <div> <b>name</b> <u ng-bind="item2.name"></u> </div> <div> <b>age</b> <i ng-bind="item2.age"></i> </div> <div> <b>stature</b> <i ng-bind="item2.stature"></i> </div> </li> </ul> <ul> <li>5 想先按年龄升序在按身高降序(全是降序了,达不到效果,见第7例)</li> <!--<li ng-repeat="item2 in items2|orderBy:'age':'-stature'">--> <li ng-repeat="item2 in items2|orderBy:'age':'stature':true"> <div> <b>name</b> <u ng-bind="item2.name"></u> </div> <div> <b>age</b> <i ng-bind="item2.age"></i> </div> <div> <b>stature</b> <i ng-bind="item2.stature"></i> </div> </li> </ul> <ul> <li>6 先按年龄在按身高排序(多个参数写出数组形式)</li> <li ng-repeat="item2 in items2|orderBy:['age','stature']"> <div> <b>name</b> <u ng-bind="item2.name"></u> </div> <div> <b>age</b> <i ng-bind="item2.age"></i> </div> <div> <b>stature</b> <i ng-bind="item2.stature"></i> </div> </li> </ul> <ul> <li>!!7 先按年龄升序在按身高降序(多个参数写出数组形式)</li> <li ng-repeat="item2 in items2|orderBy:['age','-stature']"> <div> <b>name</b> <u ng-bind="item2.name"></u> </div> <div> <b>age</b> <i ng-bind="item2.age"></i> </div> <div> <b>stature</b> <i ng-bind="item2.stature"></i> </div> </li> </ul> <ul> <li>8 日期1</li> <li ng-bind="date|date:'yyyy/MM/dd hh:mm:ss EEEE'"></li> <li>2016/11/19 11:59:05 Saturday</li> <li>8 日期2</li> <li ng-bind="date|date:'yyyy年MM月dd日 h:mma EEEE'"></li> <li>2016年11月19日 12:01PM Saturday</li> <li>8 日期3</li> <li ng-bind="date|date:'yyyy年MM月dd日 hh时mm分ss秒'"></li> <li>2016年11月22日 10时42分09秒</li> <li>8 日期4</li> <li ng-bind="date|date:'yyyy/MM/dd hh:mm:ss'"></li> <li>2016/11/22 11:16:58</li> </ul> <div>{{100000|currency:'¥'}}<!--¥可以写成$或其他--></div> <ul> <li>filter 匹配子串(以下写法只是方便观察)</li> <li>{{ webArr | filter : 'n' }} <!--匹配属性值中含有n的--></li> <li>{{ webArr | filter : 25 }} <!--匹配属性值中含有25的--></li> <li>{{ webArr | filter : {name : 'l'} }} <!--//参数是对象,匹配name属性中含有l的--></li> <li>{{ webArr | filter : fun }} <!--/参数是函数,指定返回age>25的--></li> </ul> <script src="https://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <script> var nickAppModule=angular.module('nickApp',[]); nickAppModule //求和 .filter('sumNick',function(){//管道前后所有参数和 return function(){ var arr=Array.prototype.slice.call(arguments),sum=0; for(var i= 0,len=arr.length;i<len;i++){ sum+=arr[i]?arr[i]:0; } return sum; } }) //百分比 .filter('percentNick',function(){//小数点后两位百分比 分子写在管道前面,管道后面的所有参数和加管道前的参数和为分母 return function(){ var arr=Array.prototype.slice.call(arguments),sum=0; for(var i= 0,len=arr.length;i<len;i++){ sum+=arr[i]?arr[i]:0; } //0/0也是nan return sum==0?'0%':Math.round((arr[0]?arr[0]:0)/sum*10000)/100+"%"; } }) </script> <script> nickAppModule .controller('nickCtrl',['$scope',function($scope){ $scope.items1=[{ male:66, female:23, gay:5, other:'xxx', msg:'xxx' }, { male:16, female:8, gay:7, other:'xxx', msg:'xxx' }]; $scope.items2=[ { name:'ljy', age:27, stature:165 }, { name:'nick', age:25, stature:170 }, { name:'xzl', age:27, stature:175 }, { name:'zyh', age:29, stature:165 }]; $scope.date=new Date(); $scope.webArr = [ {name:'nick',age:25}, {name:'ljy',age:28}, {name:'xzl',age:28}, {name:'zyh',age:30} ]; $scope.fun = function(e){return e.age>25;}; }]) </script> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwfgsg.html