如何使用函数过滤Angular ng-repeat集合?

时间:2016-05-13 15:22:19

标签: angularjs

我想使用| filter:function过滤ng-repeat中的项目列表。 docs有一个示例可根据输入框中的内容过滤列表。 here是对它的修改,我试图将朋友列表限制为男孩。我如何让它工作?

1 个答案:

答案 0 :(得分:1)

You can use it like this

angular.module('ngRepeat', ['ngAnimate'])
.controller('repeatController', function($scope) {
  $scope.friends = [
    {name:'John', age:25, gender:'boy'},
    {name:'Jessie', age:30, gender:'girl'},
    {name:'Johanna', age:28, gender:'girl'},
    {name:'Joy', age:15, gender:'girl'},
    {name:'Mary', age:28, gender:'girl'},
    {name:'Peter', age:95, gender:'boy'},
    {name:'Sebastian', age:50, gender:'boy'},
    {name:'Erika', age:27, gender:'girl'},
    {name:'Patrick', age:40, gender:'boy'},
    {name:'Samantha', age:60, gender:'girl'}
  ];
  $scope.justBoys = function(item, index, array) {
      console.log(item)
      return item.gender==='boy';
}
});

template only access $scope values so you have to put your function in scope