如何在AngularJS中创建过滤器
使用AngularJS,如何在数组中创建项的子集 比如,;从“TODO”创建一个新数组,该数组仅包含“done:true”项 请参见10:45左右->结束确定 仅在选中数据的情况下更新变量 您的型号:如何在AngularJS中创建过滤器,angularjs,angularjs-filter,Angularjs,Angularjs Filter,使用AngularJS,如何在数组中创建项的子集 比如,;从“TODO”创建一个新数组,该数组仅包含“done:true”项 请参见10:45左右->结束确定 仅在选中数据的情况下更新变量 您的型号: $scope.todos = [ {text:'foo' , done:false}, {text:'foobar' , done:true}, {text:'foofoo' , done:false},
$scope.todos = [
{text:'foo' , done:false},
{text:'foobar' , done:true},
{text:'foofoo' , done:false},
{text:'foobar2' , done:true}
]
您只需要检查:
$scope.todoselect = $filter('filter')($scope.todos, {done:true});
当模型更改时,您希望更新变量,所以请使用控制器中的“监视”
$scope.$watch('todos', function(newval, oldval){
if (oldval != newval)
{
$scope.todoselect = $filter('filter')($scope.todos, {done: true});
}
},true
);
});
示例:只需将过滤器添加到您的ngRepeat中,并仅显示所需内容:
ng-repeat='todo in todos | filter:done==true'
小提琴:
如果要从列表中完全删除该元素,请使用FIDLE中的archive
功能
注意:从版本1.1.3开始,过滤器行为已更改。您现在需要使用
ng-repeat='todo in todos | filter:{done:true}'
Fiddle:好的,我在中创建了一个示例。我最终实现了这一点。谢谢你的帮助。
ng-repeat='todo in todos | filter:{done:true}'