
- AngularJS 教程
- AngularJS - 首頁
- AngularJS - 概述
- AngularJS - 環境搭建
- AngularJS - MVC 架構
- AngularJS - 第一個應用
- AngularJS - 指令
- AngularJS - 表示式
- AngularJS - 控制器
- AngularJS - 過濾器
- AngularJS - 表格
- AngularJS - HTML DOM
- AngularJS - 模組
- AngularJS - 表單
- AngularJS - 包含
- AngularJS - AJAX
- AngularJS - 檢視
- AngularJS - 範圍
- AngularJS - 服務
- AngularJS - 依賴注入
- AngularJS - 自定義指令
- AngularJS - 國際化
- AngularJS 應用
- AngularJS - 待辦事項應用
- AngularJS - 記事本應用
- AngularJS - Bootstrap 應用
- AngularJS - 登入應用
- AngularJS - 上傳檔案
- AngularJS - 內聯應用
- AngularJS - 導航選單
- AngularJS - 切換選單
- AngularJS - 訂單表單
- AngularJS - 搜尋標籤
- AngularJS - 拖拽應用
- AngularJS - 購物車應用
- AngularJS - 翻譯應用
- AngularJS - 圖表應用
- AngularJS - 地圖應用
- AngularJS - 分享應用
- AngularJS - 天氣應用
- AngularJS - 定時器應用
- AngularJS - Leaflet 應用
- AngularJS - Lastfm 應用
- AngularJS 有用資源
- AngularJS - 問答
- AngularJS - 快速指南
- AngularJS - 有用資源
- AngularJS - 討論
AngularJS - 過濾器
過濾器用於修改資料。它們可以透過管道符(|)字元組合在表示式或指令中。以下列表顯示了常用的過濾器。
序號 | 名稱及描述 |
---|---|
1 | uppercase 將文字轉換為大寫文字。 |
2 | lowercase 將文字轉換為小寫文字。 |
3 | currency 將文字格式化為貨幣格式。 |
4 | filter 根據提供的條件過濾陣列到其子集。 |
5 | orderby 根據提供的條件對陣列進行排序。 |
大寫過濾器
使用管道字元將大寫過濾器新增到表示式中。這裡我們添加了大寫過濾器以全部大寫字母列印學生姓名。
Enter first name:<input type = "text" ng-model = "student.firstName"> Enter last name: <input type = "text" ng-model = "student.lastName"> Name in Upper Case: {{student.fullName() | uppercase}}
小寫過濾器
使用管道字元將小寫過濾器新增到表示式中。這裡我們添加了小寫過濾器以全部小寫字母列印學生姓名。
Enter first name:<input type = "text" ng-model = "student.firstName"> Enter last name: <input type = "text" ng-model = "student.lastName"> Name in Lower Case: {{student.fullName() | lowercase}}
貨幣過濾器
使用管道字元將貨幣過濾器新增到返回數字的表示式中。這裡我們添加了貨幣過濾器以使用貨幣格式列印費用。
Enter fees: <input type = "text" ng-model = "student.fees"> fees: {{student.fees | currency}}
過濾器
要僅顯示所需的科目,我們使用 subjectName 作為過濾器。
Enter subject: <input type = "text" ng-model = "subjectName"> Subject: <ul> <li ng-repeat = "subject in student.subjects | filter: subjectName"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul>
排序過濾器
要按分數對科目進行排序,我們使用 orderBy 分數。
Subject: <ul> <li ng-repeat = "subject in student.subjects | orderBy:'marks'"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul>
示例
以下示例顯示了所有上述提到的過濾器的用法。
testAngularJS.htm
<html> <head> <title>Angular JS Filters</title> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = "mainApp" ng-controller = "studentController"> <table border = "0"> <tr> <td>Enter first name:</td> <td><input type = "text" ng-model = "student.firstName"></td> </tr> <tr> <td>Enter last name: </td> <td><input type = "text" ng-model = "student.lastName"></td> </tr> <tr> <td>Enter fees: </td> <td><input type = "text" ng-model = "student.fees"></td> </tr> <tr> <td>Enter subject: </td> <td><input type = "text" ng-model = "subjectName"></td> </tr> </table> <br/> <table border = "0"> <tr> <td>Name in Upper Case: </td><td>{{student.fullName() | uppercase}}</td> </tr> <tr> <td>Name in Lower Case: </td><td>{{student.fullName() | lowercase}}</td> </tr> <tr> <td>fees: </td><td>{{student.fees | currency}} </td> </tr> <tr> <td>Subject:</td> <td> <ul> <li ng-repeat = "subject in student.subjects | filter: subjectName |orderBy:'marks'"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul> </td> </tr> </table> </div> <script> var mainApp = angular.module("mainApp", []); mainApp.controller('studentController', function($scope) { $scope.student = { firstName: "Mahesh", lastName: "Parashar", fees:500, subjects:[ {name:'Physics',marks:70}, {name:'Chemistry',marks:80}, {name:'Math',marks:65} ], fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + " " + studentObject.lastName; } }; }); </script> </body> </html>
輸出
在 Web 瀏覽器中開啟檔案 testAngularJS.htm。檢視結果。
廣告