AngularJS学习篇(七)

AngularJS 过滤器

过滤器能够利用二个管道字符(|)添加到表明式和下令中。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="angular-1.6.3/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">

    名: <input type="text" ng-model="firstName"><br>
    姓: <input type="text" ng-model="lastName"><br>
    <br>
    姓名: {{(firstName | lowercase) + " " + (lastName | uppercase)}}

</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.firstName = "John";
        $scope.lastName = "Doe";
    });
</script>
</body>
</html>

壹 、uppercase,lowercase 大小写转换

{{ "lower cap string" | uppercase }}   // 结果:LOWER CAP STRING
{{ "TANK is GOOD" | lowercase }}      // 结果:tank is good

2、date 格式化

{{1490161945000 | date:"yyyy-MM-dd HH:mm:ss"}} // 2017-03-22 13:52:25

③ 、number 格式化(保留小数)

{{149016.1945000 | number:2}}

四 、currency货币格式化

{{ 250 | currency }}            // 结果:$250.00
{{ 250 | currency:"RMB ¥ " }}  // 结果:RMB ¥ 250.00

5、filter查找

输入过滤器能够经过一个管道字符(|)和3个过滤器添加到指令中,该过滤器后跟二个冒号和三个模型名称。

filter 过滤器从数组中挑选二个子集

 // 查找name为iphone的行
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | filter:{'name':'iphone'} }}

6、limitTo 截取

{{"1234567890" | limitTo :6}} // 从前面开始截取6位
{{"1234567890" | limitTo:-4}} // 从后面开始截取4位

7、orderBy 排序

 // 根id降序排
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | orderBy:'id':true }}

// 根据id升序排
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | orderBy:'id' }}

相关文章