扫码一下
查看教程更方便
AngularJs 过滤器用于修改数据。它是通过在字符表达式或指令中加入竖线(|)来实现的。
下列表列出了常用的过滤器。
序号 | 过滤器 | 描述 |
---|---|---|
1 | currency | 格式化数字为货币格式。 |
2 | filter | 从数组项中选择一个子集。 |
3 | lowercase | 格式化字符串为小写。 |
4 | orderBy | 根据某个表达式排列数组。 |
5 | uppercase | 格式化字符串为大写。 |
使用管道字符向表达式添加uppercase过滤器。在这里,我们添加了 uppercase 过滤器来将所有学生的姓名转换成大写字母。
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}}
使用管道字符向表达式添加 lowercase 过滤器。在这里,我们添加了 lowercase 过滤器来将所有学生的姓名转换成小写字母。
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}}
将 currency 过滤器添加到使用管道字符返回数字的表达式。在这里,我们添加了 currency 过滤器来将费用使用货币格式打印。
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>
以下实例自定义一个过滤器 reverse,将字符串反转:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.msg = "Jiyik";
});
app.filter('reverse', function() { //可以注入依赖
return function(text) {
return text.split("").reverse().join("");
}
});
以下示例显示了上述所有过滤器的使用。
<html>
<head>
<title>Angular JS 过滤器</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>