简体   繁体   English

AngularJS-过滤对象中的选定属性

[英]AngularJS - Filter selected property in an object

Is there any way in angular $filter('filter') to filter limited property. 角度$filter('filter')有什么方法可以过滤有限的属性。

Plnkr Plnkr

HTML HTML

<body ng-controller="MyCtrl">
      <input type="type" ng-model="filter_value" />
      <ul>
        <li ng-repeat="row in data | filter: filter_value">{{row.name +'--'+ row.age}}</li>
      </ul>
    </body>

JS JS

var app = angular.module('myApp', []);
app.controller('MyCtrl', function($scope, $filter) {
  $scope.data = [
    {name: "Moroni", age: 50, secret: 'one'},
    {name: "Tiancum", age: 43, secret: 'one'},
    {name: "Jacob", age: 27, secret: 'two'},
    {name: "Nephi", age: 29, secret: 'two'},
    {name: "Enos", age: 34, secret: 'two'}
  ];
});

Problem 问题
I have a property called secret and I do not want the user to filter according to that field ie, if the user enters one or two , it should filter according to the proerty name and age and not secret . 我有一个称为secret的属性,并且我不希望用户根据该字段进行过滤,即,如果用户输入onetwo ,则应根据属性nameage过滤,而不是secret

Is there a way to do it in built-in angular filter? 有内置的角度滤镜可以做到这一点吗?

You'd have to use a custom filter to manually exclude the undesired fields. 您必须使用自定义过滤器来手动排除不需要的字段。

here's a fixed plnkr 这是固定的plnkr

html: HTML:

<body ng-controller="MyCtrl">
  <input type="type" ng-model="search.filter_value" />
  <ul>
    <li ng-repeat="row in data | usersListFilter:search.filter_value">{{row.name +'--'+ row.age}}</li>
  </ul>
</body>

javascript: JavaScript的:

app.filter('usersListFilter', usersListFilter);

function usersListFilter() {
  return function(usersArray, searchTerm){
    if (!searchTerm) return usersArray;

    var term = searchTerm.toLowerCase();
    return usersArray.filter(function(user) {
      return user.name.toLowerCase().indexOf(term) != -1 || user.age.toString().indexOf(term) != -1;
    });
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM