【问题标题】:Angular ng-repeat search for two fields of an objectAngular ng-repeat 搜索对象的两个字段
【发布时间】:2016-02-09 15:34:58
【问题描述】:

假设我有以下array

$scope.myArr = [{
    name: 'Marc Rasmussen',
    phone: 239470192,
    title: 'It Dude',
    description: 'Hello my name is Marc i am testing the fact that i can search for two fields in an object'
}, {
    name: 'Louise',
    phone: 1234567890,
    title: 'HR Director',
    description: 'I am also testing'
}, {
    name: 'Leonardo',
    phone: 123499,
    title: 'Actor',
    description: 'I have never won an oscar'
}];

现在你可以看到我有以下字段:

  • 姓名
  • 电话
  • 标题
  • 说明

现在我用ng-repeat重复这些

<div ng-repeat="obj in myArr">
    <div>
        name: {{obj.name}}
    </div> 
    <div>
        phone: {{obj.phone}}
    </div>
    <div>
        title: {{obj.title}}
    </div>
    <div>
       description: {{obj.description}}
    </div>
</div>

现在我想要的是我想要一个文本字段来搜索 namedescription 的结果,而不是其他字段

通常您会关心诸如$scope.search.$ 之类的变量,但这会搜索所有字段。或者,您可以为每个字段创建一个输入字段,但这并不能满足我希望完成的任务。

所以我的问题是如何同时对两个字段进行 1 个输入搜索?

这是上面代码的一个小提琴:

Fiddle

【问题讨论】:

    标签: javascript angularjs ng-repeat angular-filters


    【解决方案1】:

    我相信这篇文章可能会对您有所帮助: Filtering by Multiple Specific Model Properties in AngularJS (in OR relationship)

    基本上利用了 angular 的过滤器,并对重复数组中的每个项目使用谓词函数。

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 2014-05-13
      • 2017-08-20
      • 2017-06-09
      • 2017-08-11
      相关资源
      最近更新 更多