角度过滤器 - 搜索两个连接的字段

时间:2016-12-03 00:03:35

标签: javascript angularjs angularjs-filter

我有一个模型具有名字和姓氏属性的人员列表。使用角度过滤器功能,我可以创建一个基本搜索过滤列表。但是,在它的基本形式下,用户无法同时搜索名字和姓氏以获得任何结果,因为角度一次只能查看一个属性。有没有办法创建“虚拟”属性来连接另外两个用于搜索的属性?

如果我解释得非常糟糕,我通常会尝试在这个片段中搜索“史蒂夫·乔布斯”:

var app = angular.module('app', []);

app.controller('myCtrl', function($scope) {
  $scope.people = [
    {first: 'John', last: 'Smith'},
    {first: 'Jane', last: 'Doe'},
    {first: 'Bill', last: 'Gates'},
    {first: 'John', last: 'Krasinski'},
    {first: 'Ada', last: 'Lovelace'},
    {first: 'Brad', last: 'Pitt'},
    {first: 'Steve', last: 'Jobs'},
    {first: 'Bill', last: 'Clinton'},
    {first: 'Britishname', last: 'Complicated'},
    {first: 'Steve', last: 'Wozniak'}
  ];
});
<div ng-app="app" ng-controller="myCtrl">
  <input type="text" ng-model="search" placeholder="Search...">
  
  <ul>
    <li ng-repeat="person in people | filter: search">
      {{person.first}} {{person.last}}
    </li>
  </ul>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

0 个答案:

没有答案
相关问题