angular的filter
angular的filter
filter两种用法
1、在模板中使用filter
{{expression|filter}}//基本用法
{{expression|filter1|filter2|filter3}}//多个过滤器,上一段输出为下一段输入
{{expression|filter:args1,args2,...}}//带参数
除了使用简单的表达式,还可以在ng-repeat中使用,在这里我们获得处理的对象是一个数组,或者对象,不是单个的item
<span ng-repeat="a in arr|filter"></span>
2、在controller和serveice中使用filter
使用filter,在间接使用
app.controller('MyCtrl',function($scope,$filter){
$scope.num = $filter('currency')(123456);
$scope.date = $filter('date')(new Date());
});
直接使用
app.controller('MyCtrl',function($scope,currencyFilter){
$scope.num = currencyFilter(123456);
});
filter中需要注意的一点是,filter这个名字的也是一个angular本身自带的filter,用来匹配子串的。
自定义过滤器
最简单的用法
app.filter('hello',function(){
return function(input){
var str = 'hello world';
return str;
}
});
加上参数的用法
app.filter('ha',function(){
return function(input,args,args2){
var str = 'hello world'+args+args2;
return str;
}
});
filter在使用时的坑
ng-repeat中的$index
这里在使用$index就会面临,在filter出匹配的子串之后,$index的值不变化的问题,对应值就不是现在的list
<body ng-app="demo">
<p>Click the test button to see what result you would get when using <code>$index</code>
vs when using <code>item</code> directly</p>
<p>Try filtering for example for <kbd>foo</kbd> and you'll get a different result</p>
<div ng-controller="DemoCtrl">
<input type="text" ng-model="filter" placeholder="Filter the list">
<ul>
<li ng-repeat="item in items | filter:{ name: filter }">
{{item.name}}
<button ng-click="getResult($index, item)">test</button>
</li>
</ul>
<h2>Results</h2>
<p><code>$index:</code> {{indexResult.name}}</p>
<p><code>item:</code> {{itemResult.name}}</p>
</div>
</body>
var app = angular.module('demo', ['ng']);
app.controller('DemoCtrl', ['$scope', function($scope) {
$scope.items = [
{ name: 'Hello' },
{ name: 'Foobar' },
{ name: 'Barfoo' },
{ name: 'Magic' },
{ name: 'Wand' }
];
$scope.getResult = function($index, item) {
$scope.indexResult = $scope.items[$index];
$scope.itemResult = item;
};
}]);
angular的filter的更多相关文章
- angular的$filter服务
首先,介绍下$filter服务: 1.$filter是用来进行数据格式化的专用服务: 2.AngularJS内置了currency.date.filter.json.limitTo.lowercase ...
- Angular之filter学习
过滤器(filter)正如其名,作用就是接收一个输入,通过某个规则进行处理,然后返回处理后的结果.主要用在数据的格式化上,例如获取一个数组中的子集,对数组中的元素进行排序等.ng内置了一些过滤器,它们 ...
- angular 自定义filter
用modul.filter .filter("fiilterCity",function(){ return function(obj){ var newObj = []; ang ...
- angular入门--filter搜索
首先,列表绑定忽略 先上代码 <html ng-app="app1"> <head> <meta charset='utf-8' /> < ...
- 转载:Angular的filter总结
过滤器(filter)正如其名,作用就是接收一个输入,通过某个规则进行处理,然后返回处理后的结果.主要用在数据的格式化上,例如获取一个数组 中的子集,对数组中的元素进行排序等.ng内置了一些过滤器,它 ...
- [Angular 2] Filter items with a custom search Pipe in Angular 2
This lessons implements the Search Pipe with a new SearchBox component so you can search through eac ...
- Angular Input格式化
今天在Angular中文群有位同学问到:如何实现对input box的格式化.如下的方式对吗? <input type="text" ng-model="demo. ...
- Part 13 Create a custom filter in AngularJS
Custom filter in AngularJS 1. Is a function that returns a function 2. Use the filter function to cr ...
- 【转】Angular Input格式化
今天在Angular中文群有位同学问到:如何实现对input box的格式化.如下的方式对吗? <input type="text" ng-model="demo. ...
随机推荐
- adb开启不了解决方案
原文地址: adb开启不了解决方案 - vaecer - 博客频道 - CSDN.NET http://blog.csdn.net/vaecer/article/details/45894643 ...
- update多表陷阱
今天同学发了个sql题目 A1表 B1表 id num id snum 1 10 1 90 2 2000 3 4000 3 30 B表的数据插入A表当中 最后的结果 A表 1 90 2 2000 3 ...
- POJ1573——Robot Motion
Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...
- SOCKS5协议
SOCKS5 是一个代理协议,这种协议对本身所代理的内容并不关心,可用于穿越防火墙. 例如我有一台web服务器,用户可以登陆上去查询公司的关键数据,这样的服务器我肯定是不想放到公网上让别人能随便访问, ...
- pmf,cpmf,pdf,cdf,iid的解释
- ajax返回son数据
JSON 只是一种文本字符串.它被存储在 responseText 属性中 为了读取存储在 responseText 属性中的 JSON 数据,需要根据 JavaScript 的 eval 语句. 函 ...
- C语言一维数组中的数据随机排列
#include <stdio.h>#include <stdlib.h> void randomlize(int *a, int n){ int i = 0,j ...
- linux mount 命令详解
命令格式: mount [-t vfstype] [-o options] device dir 其中: 1.-t vfstype 指定文件系统的类型,通常不必指定.mount 会自动选择正确的类型. ...
- poj3041,poj2226
二分匹配的灵活运用 3041还是比较好想的,考虑到横排/竖排射一枪就能搞定这一行/一列的所有点, 我们以行数为点集x,列数为点集y,在目标点(xi,yi)之间连一条边 这样最小射击次数=最小点覆盖(边 ...
- poj12月其他题解(未完)
最近编程的时间比较少啊…… poj3253 就是个合并果子,各种优先队列即可(显然单调队列最优) poj3263 线段树统计每个点被覆盖了多少次即可,注意要去重 poj3625 最小生成树 poj36 ...