【07】AngularJS Filters

AngularJS Filters


过滤器可以使用一个管道字符(|)添加到表达式和指令中。


AngularJS 过滤器

AngularJS 过滤器可用于转换数据:

过滤器描述
currency[ˈkɜ:rənsi] 格式化数字为货币格式。
filter 从数组项中选择一个子集。
lowercase 格式化字符串为小写。
orderBy 根据某个表达式排列数组。
uppercase 格式化字符串为大写。

表达式中添加过滤器

过滤器可以通过一个管道字符(|)和一个过滤器添加到表达式中。

  1. <div ng-app="myApp" ng-controller="personCtrl">
  2.  
  3. FirstName:<input type="text" ng-model="firstName"><br>
  4. LastName:<input type="text" ng-model="lastName"><br>
  5. <br>
  6. FullName:{{firstName +" "+ lastName}}
  7.  
  8. </div>
  9.  
  10. <script src="personController.js"></script>
 
 personController.js 
  1. angular.module('myApp',[]).controller('personCtrl', function($scope){
  2. $scope.firstName ="John",
  3. $scope.lastName ="Doe",
  4. $scope.fullName = function(){
  5. return $scope.firstName +" "+ $scope.lastName;
  6. }
  7. });
 
 

uppercase 过滤器将字符串格式化为大写:

  1. <div ng-app="myApp" ng-controller="personCtrl">
  2. <p>姓名为{{ lastName | uppercase }}</p>
  3. </div>
 

lowercase 过滤器将字符串格式化为小写:

  1. <div ng-app="myApp" ng-controller="personCtrl">
  2. <p>姓名为{{ lastName | lowercase }}</p>
  3. </div>
 

currency 过滤器

currency 过滤器将数字格式化为货币格式:

  1. <div ng-app="myApp" ng-controller="costCtrl">
  2. <input type="number" ng-model="quantity">
  3. <input type="number" ng-model="price">
  4. <p>总价={{(quantity * price)| currency }}</p>
  5. </div>
 

向指令添加过滤器

过滤器可以通过一个管道字符(|)和一个过滤器添加到指令中。

orderBy 过滤器根据表达式排列数组:

  1. <div ng-app="myApp" ng-controller="namesCtrl">
  2. <ul>
  3. <li ng-repeat="x in names | orderBy:'country'">
  4. {{ x.name +', '+ x.country }}
  5. </li>
  6. </ul>
  7. <div>
 

过滤输入

输入过滤器可以通过一个管道字符(|)和一个过滤器添加到指令中,该过滤器后跟一个冒号和一个模型名称。

filter 过滤器从数组中选择一个子集:

  1. <div ng-app="myApp" ng-controller="namesCtrl">
  2. <p><input type="text" ng-model="test"></p>
  3. <ul>
  4. <li ng-repeat="x in names | filter:test | orderBy:'country'">
  5. {{(x.name | uppercase)+', '+ x.country }}
  6. </li>
  7. </ul>
  8. </div>
 
 
魔芋练习:
 
  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="renderer" content="webkit">
  6. <!--360,以webkit内核进行渲染-->
  7. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  8. <!--以最新内核进行渲染。-->
  9. <meta http-equiv="Cache-Control" content="no-siteapp"/>
  10. <!--百度禁止转码-->
  11. <title>moyu demo</title>
  12. <meta name="keywords" content="demo 测试 魔芋">
  13. <meta name="description" content="魔芋的测试示例">
  14. <meta name="robots" content="index,follow">
  15. <!--定义网页搜索引擎索引方式-->
  16. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  17. <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
  18. <script src="http://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js"></script>
  19. <style>
  20. </style>
  21. </head>
  22. <body>
  23. <div ng-app="myApp" ng-controller="myCtrl">
  24. <input type="text" ng-model="firstName">
  25. <input type="text" ng-model="lastName">
  26. <p>{{firstName +' '+ lastName}}</p>
  27. <p>{{firstName|uppercase}}</p>
  28. <p>{{price|currency}}</p>
  29. <ul>
  30. <li ng-repeat="x in moyuList|orderBy:'country'">
  31. {{x.name+","+ x.country}}
  32. </li>
  33. </ul>
  34. <p>filter</p>
  35. <p>
  36. <input type="text" ng-model="test">
  37. </p>
  38. <ul>
  39. <li ng-repeat="x in moyuList | filter:test | orderBy:'country'">
  40. {{x.name+","+ x.country}}
  41. </li>
  42. </ul>
  43. </div>
  44. <script>
  45. var app = angular.module('myApp',[]);
  46. app.controller('myCtrl',function($scope){
  47. $scope.firstName ="mo";
  48. $scope.lastName ="yu";
  49. $scope.price =123;
  50. $scope.moyuList =[{
  51. name:"上海",
  52. country:"2上海市"
  53. },{
  54. name:"湖北",
  55. country:"1武汉"
  56. },{
  57. name:"湖南",
  58. country:"3长沙"
  59. }]
  60. })
  61. </script>
  62. </body>
  63. </html>
 
结果:
123.gif
 
 
 
 
 



原文地址:https://www.cnblogs.com/moyuling/p/5206990.html