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的更多相关文章

  1. angular的$filter服务

    首先,介绍下$filter服务: 1.$filter是用来进行数据格式化的专用服务: 2.AngularJS内置了currency.date.filter.json.limitTo.lowercase ...

  2. Angular之filter学习

    过滤器(filter)正如其名,作用就是接收一个输入,通过某个规则进行处理,然后返回处理后的结果.主要用在数据的格式化上,例如获取一个数组中的子集,对数组中的元素进行排序等.ng内置了一些过滤器,它们 ...

  3. angular 自定义filter

    用modul.filter .filter("fiilterCity",function(){ return function(obj){ var newObj = []; ang ...

  4. angular入门--filter搜索

    首先,列表绑定忽略 先上代码 <html ng-app="app1"> <head> <meta charset='utf-8' /> < ...

  5. 转载:Angular的filter总结

    过滤器(filter)正如其名,作用就是接收一个输入,通过某个规则进行处理,然后返回处理后的结果.主要用在数据的格式化上,例如获取一个数组 中的子集,对数组中的元素进行排序等.ng内置了一些过滤器,它 ...

  6. [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 ...

  7. Angular Input格式化

    今天在Angular中文群有位同学问到:如何实现对input box的格式化.如下的方式对吗? <input type="text" ng-model="demo. ...

  8. 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 ...

  9. 【转】Angular Input格式化

    今天在Angular中文群有位同学问到:如何实现对input box的格式化.如下的方式对吗? <input type="text" ng-model="demo. ...

随机推荐

  1. ColorBox常见问题

    发现colorbox官方网站的troubleshoot写的比较好,转载一下. 1,flash覆盖colorbox: This is not a ColorBox specific problem, b ...

  2. 【iOS开发】iOS7 兼容及部分细节

    1:statusBar字体为白色 在plist里面设置View controller-based status bar appearance 为 NO:设置statusBarStyle 为 UISta ...

  3. Android 开发之 ---- 底层驱动开发(一)

    驱动概述 说到 android 驱动是离不开 Linux 驱动的.Android 内核采用的是 Linux2.6 内核 (最近Linux 3.3 已经包含了一些 Android 代码).但 Andro ...

  4. 手机通过WIFI连上ZXV10 H618B路由器但不能上网问题的解决

    前几天朋友帮忙拿到一个ZXV10 H618B路由器,一看需要12V供电,还好以前留下一个12V输出的DC充电器,关键时刻用上了,先大概下载了此路由器的用户手册,发现原来是08年的产品,都5年了. 开始 ...

  5. 不只是技术!成为IT经理必备的十大软技能

    摘要:可能你是一名普通的IT从业员,一个小小的程序员,可随着社会的发展和科技的进步,对人才的要求越来越高,你可能通过技术获得了职位,但你若想升职加薪却少不了软技能:谈判技巧.积极倾听.演讲技巧以及领导 ...

  6. VC++6.0环境下调试c语言代码的方法和步骤_附图

    1.C语言程序四步开发步骤 (1)编辑.可以用任何一种编辑软件将在纸上编写好的C语言程序输入计算机,并将C语言源程序文件*.c以纯文本文件形式保存在计算机的磁盘上(不能设置字体.字号等). (2)编译 ...

  7. 在ubuntu14.04上编译android4.2.2系统

    原创作品,转载请注明出处,严禁非法转载             copyright:weishusheng   2015.8.1 时下android系统非常流行,下面就来说一下,下载编译一个andro ...

  8. css选择器,有箭头与没箭头的区别

    div > span 和 div span 的区别 ,即有箭头和没箭头的区别 div > span span 是 div 的下一层级关系 在这种情况下找得到span元素: <div& ...

  9. 17、手势(Gesture)

    课程目标: 学习Android必不可少的手势的功能 了解手势识别原理 , 掌握制作,加载以及识别手势 写出自己的手势Demo 重点难点:手势机制的了解     手势库的制作 考核目标:请说一下手势库的 ...

  10. opencv linux

    This link which you also mentioned describes the necessary steps to compile OpenCV on your machine. ...