angularjs-過濾器
阿新 • • 發佈:2017-08-16
angularjs
#angularjs常用過濾器 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="js/angular.min.js"></script> </head> <body> <div ng-app="app" ng-controller="ctrl"> 貨幣currency:{{999.99|currency:‘$‘:1}} <br /> 數字number:{{999.111|number:2}} <br /> 大寫lowercase:{{"CXIONG"|lowercase}} <br /> 小寫uppercase: {{"cxiong"|uppercase}} <br /> 截取字符limitTo:{{"cxiong"|limitTo:2:1}} <br /> 日期data:{{time|date:‘yyyy年MM月dd日 HH時mm分ss秒‘}} <br /> 排序orderBy:{{data|orderBy}} <br /> <!--filter精確匹配--> 過濾filter:{{data|filter:1:true}} <br /> </div> <script type="text/javascript"> var m=angular.module(‘app‘,[]); m.controller(‘ctrl‘,[‘$scope‘,function($scope){ $scope.time=new Date().getTime() $scope.data=[1,3,4,55,66,23,14,41] }]); </script> </body> </html>
#$filter和callee應用表格排序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src=‘js/angular.min.js‘> </script> <style type="text/css"> .ng-cloak{display:none;} </style> </head> <body ng-app="hd" ng-cloak class="ng-cloak"> <div ng-controller="ctrl"> <table border="" cellspacing="" cellpadding=""> <tr> <th ng-click="sort(‘name‘)">名稱</th> <th ng-click="sort(‘num‘)">數量</th> <th ng-click="sort(‘price‘)">價格</th> </tr> <tr ng-repeat="d in data"> <td>{{d.name}}</td> <td>{{d.num}}</td> <td>{{d.price}}</td> </tr> </table> </div> <script type="text/javascript"> var m=angular.module(‘hd‘,[]) m.controller(‘ctrl‘,[‘$scope‘,‘$filter‘,function($scope,$filter){ $scope.data=[ {‘name‘:‘iphone6‘,‘num‘:150,‘price‘:3999}, {‘name‘:‘beats‘,‘num‘:100,‘price‘:999}, {‘name‘:‘iphone7‘,‘num‘:500,‘price‘:5999}, {‘name‘:‘ipad‘,‘num‘:250,‘price‘:1999} ] var status=true $scope.sort=function(field){ //arguments.callee 全局存放靜態變量 if (arguments.callee[field]==‘undefine‘) { arguments.callee[field]=true } arguments.callee[field]=!arguments.callee[field] $scope.data=$filter(‘orderBy‘)($scope.data,field,arguments.callee[field]) } }]) </script> </body> </html>
angularjs-過濾器