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. ...
随机推荐
- Extension Method[上篇]
在C#3.0中,引入了一些列新的特性,比如: Implicitly typed local variable, Extension method,Lambda expression, Object i ...
- linux 查看系统信息命令
linux 查看系统信息命令是linux初学者必备的基础知识, 这些命令也非常有用, 因为进入linux第一件事就可能是首先查看系统信息, 因此必要的系统的学习一下这些linux系统信息命令还是非常有 ...
- git设置忽略某些文件或文件夹
在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法.如果没有 .gitignore 文件,就自己创建一个,手动创建会提示你输入文件名称,因此,你 ...
- URAL1501. Sense of Beauty(记忆化)
链接 dfs+记忆化 对于当前状态虽然满足和差 但如果搜下去没有满足的情况也是不可以的 所以需要记忆化下 #include <iostream> #include<cstdio> ...
- vijos1049送给圣诞夜的礼品
这题犯了两个sb错误,写下来,为以后做个警告 一.mul过程中将k作为了循环变量 二.看错了题…… 题目中说是数到k行,而我却以为数k遍…… 做矩阵乘法,只要记住一句话:置换一定可以写成矩阵的形式! ...
- SHA-1加密
/** * SHA-1加密 * @param strSrc 要加密的字符串 * @return 加密后的字符串 */ public static String SHAEncrypt(String st ...
- shell脚本编写笔记
包含转载内容,转载自http://wenku.baidu.com/link?url=jtCHxEYzgGve6P64U3JRQRgU6nhpGvqFLLpWu9I2Htq6hi9TGLudRFkk7r ...
- jQuery基础知识— 获得内容和属性
jQuery拥有可操作HTML元素和属性的方法. 获得内容: text()--设置或返回所选元素的文本内容 html()--设置或返回所选元素的内容(包括HTML标记) val()--设置或返回表 ...
- 设计模式_Iterator_迭代器模式
形象例子: 我爱上了Mary,不顾一切的向她求婚. Mary:“想要我跟你结婚,得答应我的条件” 我:“什么条件我都答应,你说吧” Mary:“我 ...
- 设计模式_Command_命令模式
形象例子: 俺有一个MM家里管得特别严,没法见面,只好借助于她弟弟在我们俩之间传送 信息,她对我有什么指示,就写一张纸条让她弟弟带给我.这不,她弟弟又传送过来一个COMMAND,为了感谢他,我请他吃了 ...