AngularJS 1.x系列:AngularJS过滤器(4)
1. AngularJS过滤器(Filter)使用方法
AngularJS中过滤器(Filter)主要功能是格式化数据。
AngularJS过滤器使用方法有3种:
◊ 在表达式{{}}中使用
◊ 在指令中使用
◊ 在Controller或Service、Factory、Provider等服务中使用
1.1 表达式中使用过滤器
过滤器使用管道符(|)添加到表达式或指令中。
语法:
{{ expression | filter }}
可以同时使用多个过滤器,多个过滤器之间管道符(|)分隔。
{{ expression | filter1 | filter2 | ... | filterN }}
过滤器可以带参数,参数使用冒号(:)隔开。
{{ expression | filter1:param1 | filter2:param2 | ... | filterN:paramN }}
1.2 指令中使用过滤器
过滤器使用管道符(|)添加到表达式或指令中。
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<script src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("ProductCtrl", ["$scope", function($scope){
$scope.data = [
{ ProductName: "家电", Quantity: 600 },
{ ProductName: "数码", Quantity: 200 },
{ ProductName: "手机", Quantity: 300 }
];
}]);
</script>
</head>
<body>
<ul ng-controller="ProductCtrl">
<li ng-repeat="item in data | orderBy: 'Quantity'">
<span>{{ item.ProductName }}</span>
<span>{{ item.Quantity }}</span>
</li>
</ul>
</body>
</html>
降序:
<li ng-repeat="item in data | orderBy: 'Quantity' : true">
1.3 在Controller或AngularJS服务中使用过滤器
在Controller、Service、Factory、Provider等服务中使用过滤器方式:依赖注入。
示例:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("MainCtrl", ["$scope", "currencyFilter", function($scope, currencyFilter) {
$scope.unitprice = currencyFilter(10000);
}]);
</script>
</head>
<body>
<div ng-controller="MainCtrl">
{{ unitprice }}
</div>
</body>
</html>
currencyFilter为AngularJS内置过滤器,用于将数字转化为货币形式。
AngularJS提供$filter服务,可以调用所有过滤器。
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("MainCtrl", ["$scope", "$filter", function($scope, $filter) {
$scope.unitprice = $filter("currency")(10000);
$scope.now = $filter("date")(new Date(), "yyyy-MM-dd HH:mm:ss");
}]);
</script>
</head>
<body>
<div ng-controller="MainCtrl">
{{ unitprice }} // $10,000.00
{{ now }} // 2017-06-05 23:08:02
</div>
</body>
</html>
$scope.unitprice = $filter("currency")(, "¥"); // ¥10,000.00
$filter("name")根据名称获取对应的过滤器,把数据作为参数传递给过滤器处理。
2. AngularJS内置过滤器
2.1 filter
filter过滤器用来处理数组,过滤包含某个子串的元素,过滤后的元素作为子数组返回。可以是字符串数组或对象数组。
对象数组,则匹配属性的值。可以接收一个参数,用来定义子元素的匹配规则。
示例:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("ProductCtrl", ["$scope", function($scope){
$scope.data = [
{ ProductName: "家电", Quantity: 1000, UnitPrice: 100 },
{ ProductName: "数码", Quantity: 2000, UnitPrice: 200 },
{ ProductName: "手机", Quantity: 3000, UnitPrice: 300 }
];
}]);
</script>
</head>
<body>
<div class="container">
<div class="row">
<input type="text" class="form-control" ng-model="keyword" placeholder="请输入查询关键字" />
</div>
<table class="table table-bordered table-hover" ng-controller="ProductCtrl">
<thead>
<tr>
<th class="text-center">商品名称</th>
<th class="text-center">数量</th>
<th class="text-center">单价</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data | filter: keyword">
<td>{{ item.ProductName }}</td>
<td class="text-center">{{ item.Quantity }}</td>
<td class="text-center">{{ item.UnitPrice }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("ProductCtrl", ["$scope", function($scope){
$scope.data = [
{ ProductName: "家电", Quantity: 1000, UnitPrice: 100 },
{ ProductName: "数码", Quantity: 2000, UnitPrice: 200 },
{ ProductName: "手机", Quantity: 3000, UnitPrice: 300 }
];
}]);
</script>
</head>
<body>
<div class="container">
<table class="table table-bordered table-hover" ng-controller="ProductCtrl">
<thead>
<tr>
<th class="text-center">商品名称</th>
<th class="text-center">数量</th>
<th class="text-center">单价</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data | filter: {'ProductName': '数'}">
<td>{{ item.ProductName }}</td>
<td class="text-center">{{ item.Quantity }}</td>
<td class="text-center">{{ item.UnitPrice | currency }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
2.2 currency
currency货币格式化,默认为$,可以参数设置其他符号及精度。
示例:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("ProductCtrl", ["$scope", function($scope){
$scope.data = [
{ ProductName: "家电", Quantity: 1000, UnitPrice: 100 },
{ ProductName: "数码", Quantity: 2000, UnitPrice: 200 },
{ ProductName: "手机", Quantity: 3000, UnitPrice: 300 }
];
}]);
</script>
</head>
<body>
<div class="container">
<div class="row">
<input type="text" class="form-control" ng-model="keyword" placeholder="请输入查询关键字" />
</div>
<table class="table table-bordered table-hover" ng-controller="ProductCtrl">
<thead>
<tr>
<th class="text-center">商品名称</th>
<th class="text-center">数量</th>
<th class="text-center">单价</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data | filter: keyword">
<td>{{ item.ProductName }}</td>
<td class="text-center">{{ item.Quantity }}</td>
<td class="text-center">{{ item.UnitPrice | currency }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
设置其他符号:
{{ 1000 | currency: "¥" }} // ¥1,000.00
设置精度:
{{ 1000.75 | currency: "¥": 1 }} // ¥1,000.8
2.3 number
number过滤器用于将数字格式化为文本,可以对数字设置精度。默认保留的小数位数小于或等于3。
{{ 10000 | number }} // 10,000
{{ 3.1415926 | number }} // 3.142
{{ 3.1415926 | number: 2 }} // 3.14
2.4 lowercase & uppercase
lowercase过滤器:将字符串中的大写字母全部转换为小写。
{{ "Hello AngularJS" | lowercase }} // hello angularjs
uppercase过滤器:将字符串中的小写字母全部转换为大写。
{{ "Hello AngularJS" | uppercase }} // HELLO ANGULARJS
2.5 date
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("DateCtrl", ["$scope", function($scope){
$scope.now = new Date();
}]);
</script>
</head>
<body>
<div ng-controller="DateCtrl">
<span>{{ now | date }}</span>
<span>{{ now | date: "yyyy-MM-dd" }}</span>
<span>{{ now | date: "yyyy-MM-dd HH:mm:ss" }}</span>
<span>{{ now | date: "yyyy年MM月dd日" }}</span>
</div>
</body>
</html>
2.5 limitTo
limitTo过滤器:用来截取数组或字符串,参数用来指定截图长度。如果参数是负值,则从尾部开始截取。
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("ProductCtrl", ["$scope", function($scope){
$scope.data = [
{ ProductName: "家电", Quantity: 1000, UnitPrice: 100 },
{ ProductName: "数码", Quantity: 2000, UnitPrice: 200 },
{ ProductName: "手机", Quantity: 3000, UnitPrice: 300 }
];
}]);
</script>
</head>
<body>
<div class="container">
<table class="table table-bordered table-hover" ng-controller="ProductCtrl">
<thead>
<tr>
<th class="text-center">商品名称</th>
<th class="text-center">数量</th>
<th class="text-center">单价</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data | limitTo: 2">
<td>{{ item.ProductName }}</td>
<td class="text-center">{{ item.Quantity }}</td>
<td class="text-center">{{ item.UnitPrice | currency }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
{{ "Hello" | limitTo: 2 }} // He
2.6 orderBy
orderBy过滤器:用来将一个数组中的元素进行排序,可以设置参数指定排序规则。
参数:字符串,表示按照该属性名进行排序。
参数:数组,表示依次按照数组中的属性进行排序。
示例:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("ProductCtrl", ["$scope", function($scope){
$scope.data = [
{ ProductName: "家电", Quantity: 5000, UnitPrice: 500 },
{ ProductName: "数码", Quantity: 2000, UnitPrice: 200 },
{ ProductName: "手机", Quantity: 3000, UnitPrice: 300 }
];
}]);
</script>
</head>
<body>
<div class="container">
<table class="table table-bordered table-hover" ng-controller="ProductCtrl">
<thead>
<tr>
<th class="text-center">商品名称</th>
<th class="text-center">数量</th>
<th class="text-center">单价</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data | orderBy: 'Quantity' | limitTo: 2">
<td>{{ item.ProductName }}</td>
<td class="text-center">{{ item.Quantity }}</td>
<td class="text-center">{{ item.UnitPrice | currency }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
orderBy过滤器默认为升序。
设置降序两种方式:
<tr ng-repeat="item in data | orderBy: 'Quantity': true | limitTo: 2">
<tr ng-repeat="item in data | orderBy: '-Quantity' | limitTo: 2"> // 排序属性前加减号(-)
3. 自定义过滤器
AngularJS自定义过滤器,调用模块实例的filter()方法。
3.1 自定义不带参数过滤器
filter()使用方法:
var app = angular.module("app", []);
app.filter("filterName", function() {
return function(input) {
// 逻辑处理
...
return result;
};
});
filter()方法两个参数:
第一个参数:过滤器名称,
第二个参数:过滤器定义方法,必须返回一个用于处理过滤器逻辑的方法。返回的方法可接受一个参数:过滤器的输出数据。
示例:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("MainCtrl", ["$scope", function ($scope) {
$scope.amount = 1234.53;
}]);
app.filter("toRMB", function () {
return function (input) {
var fraction = ["角", "分"];
var digit = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
var unit = [["元", "万", "亿"], ["", "拾", "佰", "仟"]]; var result = "整";
for (var i = 0; i < fraction.length; i++) {
var t = Math.floor(input * 10 * Math.pow(10, 1)) % 10;
result += (digit[Math.floor(input * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, "");
} result = result || "整"; input = Math.floor(input);
for (var i = 0; i < unit[0].length && input > 0; i++) {
var n = "";
for (var j = 0; j < unit[1].length && input > 0; j++) {
n = digit[input % 10] + unit[1][j] + n;
input = Math.floor(input / 10);
} result = n.replace(/(零.)*零$/, "").replace(/^$/, "零") + unit[0][i] + result;
} result = result.replace(/(零.)*零元/, "元").replace(/(零.)+/g, "零").replace(/^整$/, "零元整"); return result;
};
});
</script>
</head>
<body>
<div ng-controller="MainCtrl">
{{ amount | toRMB }}
</div>
</body>
</html>
注:toRMB数字转大写方法存在Bug,不能完全正确转换。
使用$filter调用自定义过滤器:
app.controller("MainCtrl", ["$scope", "$filter", function ($scope, $filter) {
$scope.amount = $filter("toRMB") (1234.53);
}]);
3.2 自定义带参数的过滤器
语法:
app.filter("filterName", function() {
return function(input, param1, param2, ... ,paramN) {
// 逻辑处理
...
return result;
};
});
其中:param1,param2,...,paramN为过滤器使用时传入的参数。
示例:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.controller("ProductCtrl", ["$scope", function($scope){
$scope.data = [
{ ProductName: "家电", Quantity: 5000, UnitPrice: 500 },
{ ProductName: "数码", Quantity: 2000, UnitPrice: 200 },
{ ProductName: "手机", Quantity: 3000, UnitPrice: 300 }
];
}]);
app.filter("greaterThan", function() {
return function(input, unitPrice) {
var result = [];
if(typeof(unitPrice) != "undefined" && unitPrice != "") {
angular.forEach(input, function(item) {
if(item.UnitPrice > unitPrice) {
result.push(item);
}
}); return result;
} return input;
};
});
</script>
</head>
<body>
<div class="container">
<div class="row form-inline" style="padding: 5px 15px;">
单价:<input type="text" class="form-control" ng-model="unitprice" style="width: 200px;" />
</div>
<table class="table table-bordered table-hover" ng-controller="ProductCtrl" style="width: 500px;">
<thead>
<tr>
<th class="text-center">商品名称</th>
<th class="text-center">数量</th>
<th class="text-center">单价</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data | greaterThan: unitprice">
<td>{{ item.ProductName }}</td>
<td class="text-center">{{ item.Quantity }}</td>
<td class="text-center">{{ item.UnitPrice | currency }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
4. 第三方过滤器插件
4.1 angular-filter
官网:https://github.com/a8m/angular-filter
npm安装:
npm install angular-filter
示例:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript" src="../lib/angular-filter.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", ["angular.filter"]);
app.controller("ProductCtrl", ["$scope", function ($scope) {
$scope.data = [
{ ProductName: "家电", Quantity: 5000, UnitPrice: 500 },
{ ProductName: "数码", Quantity: 2000, UnitPrice: 200 },
{ ProductName: "手机", Quantity: 3000, UnitPrice: 300 }
];
}]);
</script>
</head>
<body>
<div class="container">
<table class="table table-bordered table-hover" ng-controller="ProductCtrl">
<thead>
<tr>
<th class="text-center">商品名称</th>
<th class="text-center">数量</th>
<th class="text-center">单价</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data | first: 1">
<td>{{ item.ProductName }}</td>
<td class="text-center">{{ item.Quantity }}</td>
<td class="text-center">{{ item.UnitPrice | currency }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
其中,引入插件:
<script type="text/javascript" src="../lib/angular-filter.min.js"></script>
在自定义模块中添加angular.filter注入:
var app = angular.module("app", ["angular.filter"]);
angular-filter部分过滤器示例:
集合元素
unique:从数组或对象中删除重复项。
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript" src="../lib/angular-filter.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", ["angular.filter"]);
app.controller("ProductCtrl", ["$scope", function ($scope) {
$scope.data = [
{ ProductName: "家电", Quantity: 5000, UnitPrice: 500 },
{ ProductName: "数码", Quantity: 2000, UnitPrice: 300 },
{ ProductName: "手机", Quantity: 3000, UnitPrice: 300 }
];
}]);
</script>
</head>
<body>
<div class="container">
<table class="table table-bordered table-hover" ng-controller="ProductCtrl">
<thead>
<tr>
<th class="text-center">商品名称</th>
<th class="text-center">数量</th>
<th class="text-center">单价</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data | unique: 'UnitPrice'">
<td>{{ item.ProductName }}</td>
<td class="text-center">{{ item.Quantity }}</td>
<td class="text-center">{{ item.UnitPrice | currency }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
AngularJS 1.x系列:AngularJS过滤器(4)的更多相关文章
- [后端人员耍前端系列]AngularJs篇:30分钟快速掌握AngularJs
一.前言 对于前端系列,自然少不了AngularJs的介绍了.在前面文章中,我们介绍了如何使用KnockoutJs来打造一个单页面程序,后面一篇文章将介绍如何使用AngularJs的开发一个单页面应用 ...
- Angular系列----AngularJS入门教程05:双向绑定(转载)
在这一步你会增加一个让用户控制手机列表显示顺序的特性.动态排序可以这样实现,添加一个新的模型属性,把它和迭代器集成起来,然后让数据绑定完成剩下的事情. 请重置工作目录: git checkout -f ...
- AngularJS 1.x系列:AngularJS服务-Service
1. AngularJS服务 AngularJS可注入类型包括:Service.Factory.Provider.Value及Constant. 2. Service AngularJS Servic ...
- Angular系列----AngularJS入门教程00:引导程序(转载)
我们现在开始准备编写AngularJS应用——phonecat.这一步骤(步骤0),您将会熟悉重要的源代码文件,学习启动包含AngularJS种子项目的开发环境,并在浏览器端运行应用. 进入angul ...
- Angular系列------AngularJS快速开始(转载)
Hello World! 开始学习AngularJS的一个好方法是创建经典应用程序“Hello World!”: 使用您喜爱的文本编辑器,创建一个HTML文件,例如:helloworld.html. ...
- AngularJS 1.x系列:AngularJS简介及第一个应用(2)
1. 安装AngularJS 1.1 AngularJS官网 Github源码:https://github.com/angular/angular.js 官网:https://angularjs.o ...
- AngularJS 1.x系列:AngularJS服务-Service、Factory、Provider、Value及Constant(5)
1. AngularJS服务 AngularJS可注入类型包括:Service.Factory.Provider.Value及Constant. 2. Service AngularJS Servic ...
- AngularJS学习笔记2——AngularJS的初始化
本文主要介绍AngularJS的自动初始化以及在必要的适合如何手动初始化. Angular <script> Tag 下面通过一小段代码来介绍推荐的自动初始化过程: <!doctyp ...
- AngularJS学习之旅—AngularJS 表达式(二)
1.AngularJS 表达式 AngularJS 表达式写在双大括号内:{{ expression }}. AngularJS 表达式把数据绑定到 HTML,这与 ng-bind 指令有异曲同工之妙 ...
- AngularJS学习笔记二:AngularJS指令
AngularJS 指令: AngularJS 通过被称为 指令 的新属性来扩展 HTML. AngularJS 指令是扩展的 HTML 属性,带有前缀 ng-. 几个常用 指令: ng-app 指令 ...
随机推荐
- Guava Cache 本地缓存组件浅析
cache组件中核心的类和接口列举如下: 接口: Cache 本地缓存的顶级接口,提供一些对缓存进行get,put的方法,以及获取缓存统计数据的方法等. LoadingCache 继承了Cache接口 ...
- Java集合类源码解析:AbstractList
今天学习Java集合类中的一个抽象类,AbstractList. 初识AbstractList AbstractList 是一个抽象类,实现了List<E>接口,是隶属于Java集合框架中 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- vue项目利用apicloud打包成apk过程
最近公司要求我们用apicloud做一个app,正好利用这个机会学习下app的制作过程~ 页面的开发过程跟我们平时开发一样,利用vue把页面全部完成,最后进行npm run build将项目打包. 接 ...
- 使用代码检查Dynamics 365中的备用键状态
摘要: 微软动态CRM专家罗勇 ,回复304或者20190213可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 备用键(Al ...
- Dynamics 365-如何下载新版本的Tools
最近新写了个Plugin,想注册到CRM中,但是使用旧版的PluginRegistration Tool的时候,总是加载不出来Plugin数据,所以寻思下载个最新版的Tool试试.现在下载Tools的 ...
- Idea软件中隐藏忽略.idea,.iml等文件
参考链接,https://blog.csdn.net/fanrenxiang/article/details/80533950 ignore files and folders;增加隐藏的文件类型:
- kafka环境搭建
kafka环境搭建 for mac 对应qq群号:616961231 在之前的文章中, 有学习能力和兴趣爱好的同学,自己动手维护测试环境,丰衣足食是最好的办法,今天我们来讲讲kafka在mac上的安装 ...
- net view 提示6118错误 解决方法。
1.win+R ,输入services.msc 开启服务:Server ,WorkStation,computer Browser 2.如果你的电脑没有computer Browser服务,win+R ...
- httpservlet里单纯分页
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletExcep ...