AngularJS 过滤器
可以在 AngularJS 中添加过滤器来格式化数据。
AngularJS 过滤器
AngularJS 提供用于转换数据的筛选器:
currency
将数字格式化为货币格式。date
将日期格式化为指定格式。filter
从数组中选择项目的子集。json
将对象格式化为JSON字符串。limitTo
将数组/字符串限制为指定数量的元素/字符。lowercase
将字符串格式化为小写。number
将数字格式化为字符串。orderBy
通过表达式对数组进行排序。uppercase
将字符串格式化为大写。
表达式增加过滤器
可以使用管道字符 |
,后跟过滤器,将过滤器添加到表达式中。
大写过滤器将字符串格式化为大写:
实例
<!DOCTYPE html>
<html>
<script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
<p>名称是 {{ lastName | uppercase }}</p>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
});
</script>
</body>
</html>
lowercase
过滤器将字符串格式化为小写:
实例
<!DOCTYPE html>
<html>
<script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
<p>名称是 {{ lastName | lowercase }}</p>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
});
</script>
</body>
</html>
指令添加过滤器
通过使用管道字符 |
,后跟过滤器,将过滤器添加到指令中,如 ng-repeat
:
实例
orderBy
过滤器对数组进行排序:
<!DOCTYPE html>
<html>
<script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<p>循环对象:</p>
<ul>
<li ng-repeat="x in names | orderBy:'country'">
{{ x.name + ', ' + x.country }}
</li>
</ul>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
});
</script>
</body>
</html>
货币过滤器
currency
过滤器将数字格式化为货币
实例
<!DOCTYPE html>
<html>
<script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="costCtrl">
<h2>价格: {{ price | currency }}</h2>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('costCtrl', function($scope) {
$scope.price = 58;
});
</script>
<p>货币过滤器将数字格式化为货币格式。</p>
</body>
</html>
filter 过滤器
filter
过滤器选择数组的子集。
filter
过滤器只能用于数组,它返回一个只包含匹配项的数组。
实例
返回包含字母 "i" 的名称:
<!DOCTYPE html>
<html>
<script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<ul>
<li ng-repeat="x in names | filter : 'i'">
{{ x }}
</li>
</ul>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
</script>
<p>本例仅显示包含字母 "i" 的名称。</p>
</body>
</html>
根据用户输入过滤数组
通过在输入字段上设置 ng-model
指令,我们可以将输入字段的值用作过滤器中的表达式。
在输入字段中键入一个字母,列表将根据匹配情况收缩/增长:通过在表头上添加 ng-click
指令,我们可以运行一个函数来更改数组的排序顺序:
实例
<!DOCTYPE html>
<html>
<script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<p>在输入框中输入一个字母:</p>
<p><input type="text" ng-model="test"></p>
<ul>
<li ng-repeat="x in names | filter:test">
{{ x }}
</li>
</ul>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
</script>
<p>该列表将只包含与过滤器匹配的名称。</p>
</body>
</html>
自定义过滤器
您可以通过在模块中注册新的过滤器工厂功能来制作自己的过滤器:
实例
做一个名叫 "myFormat" 的自定义过滤器:
<!DOCTYPE html>
<html>
<script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<body>
<p>单击表格标题以更改排序顺序:</p>
<div ng-app="myApp" ng-controller="namesCtrl">
<table border="1" width="100%">
<tr>
<th ng-click="orderByMe('name')">名称</th>
<th ng-click="orderByMe('country')">国家</th>
</tr>
<tr ng-repeat="x in names | orderBy:myOrderBy">
<td>{{x.name}}</td>
<td>{{x.country}}</td>
</tr>
</table>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
$scope.orderByMe = function(x) {
$scope.myOrderBy = x;
}
});
</script>
</body>
</html>
myFormat
过滤器将其他字符格式化为大写。