AngularJS過濾器詳解及示例代碼
過濾器是用來更改修改數(shù)據(jù),并且可以在表達式或使用管道符指令將其歸入。以下是常用的過濾器的列表。
S.No. | 名稱 | 描述 |
---|---|---|
1 | 大寫 | 轉(zhuǎn)換文本為大寫文本。 |
2 | 小寫 | 轉(zhuǎn)換文本為小寫文本。 |
3 | 貨幣 | 貨幣格式格式文本。 |
4 | 過濾器 | 過濾數(shù)組中它根據(jù)所提供的標(biāo)準(zhǔn)的一個子集。 |
5 | 排序 | 排序提供標(biāo)準(zhǔn)的基礎(chǔ)數(shù)組。 |
大寫過濾器
添加大寫的過濾器使用管道符的表達式。在這里,添加了大寫過濾器,全部用大寫字母打印學(xué)生姓名。
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}}
小寫過濾器
添加小寫的過濾器,使用管道符的表達式。在這里添加小寫過濾器,以小寫字母打印學(xué)生姓名。
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() | lowercase}}
貨幣濾波器
加幣過濾器使用管道符返回數(shù)的表達式。在這里,我們添加了過濾器,貨幣使用貨幣格式的打印費用。
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>
排序過濾器
要通過標(biāo)記排序主題,我們使用orderBy標(biāo)記。
Subject: <ul> <li ng-repeat="subject in student.subjects | orderBy:'marks'"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul>
例子
下面的例子將展示上述所有的過濾器。
testAngularJS.html
<html> <head> <title>Angular JS Filters</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app="" 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> function studentController($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> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> </body> </html>
輸出
在Web瀏覽器打開textAngularJS.html,看到以下結(jié)果:
以上就是AngularJS過濾器的資料整理,后續(xù)繼續(xù)補充相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
如何使用AngularJs打造權(quán)限管理系統(tǒng)【簡易型】
這篇文章主要介紹了使用AngularJs打造權(quán)限管理系統(tǒng)【簡易型】的相關(guān)資料,需要的朋友可以參考下2016-05-05Angular基于Constructor?Parameter的依賴注入方式詳解
這篇文章主要為大家介紹了Angular基于Constructor?Parameter的依賴注入方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11angular2 ng2 @input和@output理解及示例
本篇文章主要介紹了angular2 ng2 @input和@output理解及示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Angular 數(shù)據(jù)請求的實現(xiàn)方法
本篇文章主要介紹了Angular 數(shù)據(jù)請求的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05