如何在AngularJS中创建过滤器

spu*_*der 2 angularjs angularjs-filter

使用AngularJS,如何在数组中创建项的子集?

例如; 从"todos"创建一个新数组,该数组仅包含"done:true"项.

function fooController ($scope) {

$scope.todos = [
        {text:'foo'        , done:false}, 
        {text:'foobar'     , done:true},
        {text:'foofoo'     , done:false}, 
        {text:'foobar2'    , done:true}
 ]

}
Run Code Online (Sandbox Code Playgroud)

http://docs.angularjs.org/api/ng.filter:filter

http://www.youtube.com/watch?v=WuiHuZq_cg4&list=PL173F1A311439C05D&context=C48ac877ADvjVQa1PpcFONnl4Q5x8hqvT6tRBTE-m0-Ym47jO3PEE%3D

见约10:45 - >结束

Tim*_*ers 5

只需将过滤器添加到ngRepeat并仅显示您想要的内容:

ng-repeat='todo in todos | filter:done==true'
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/dPWsv/1/

如果要从列表中完全删除元素,请使用archive小提琴中的函数.

注意:从版本1.1.3开始,过滤器行为已更改.你现在想要使用

ng-repeat='todo in todos | filter:{done:true}'
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/UdbVL/1/