【问题标题】:AngularJs: Searching using filter option is not working properlyAngularJs:使用过滤器选项搜索无法正常工作
【发布时间】:2013-06-19 07:18:04
【问题描述】:

我对 angularjs 中的过滤文本有疑问。

我有一个名为“users”的联系人 JSON 数组

[{ "contactId": 1, "contactName": "Anil", "contactEmail": "anil@domain.com", "contactMobile": "1111111111" }, { "contactId": 2, "contactName": "Pankaj", "contactEmail": "pankaj@domain.com", "contactMobile": "2222222222" }, { "contactId": 3, "contactName": "John", "contactEmail": "john@domain.com", "contactMobile": "333444555" }]

用于搜索过滤器的 HTML

<div>
  <label>Name:</label>
  <input type="text" ng-model="nameFilter" placeholder="Enter a name here">
  <hr>
  <h1>Hello {{nameFilter}}!</h1>
<ul ng-controller='MyController'>
    <li ng-repeat="user in users | filter:nameFilter">{{ user.contactName }}</li>
</ul>
</div>

在 HTML 中,我只打印了contactName,因为我只想使用上面的文本框过滤“姓名”,而不是使用contactEmail 或contactMobile ...但是过滤选项是使用contactEmail 和contactMobile 过滤数据,因为JSON 具有这些对象。 ...但如果不从 JSON 数组中删除 json 对象(contactEmail 和 contactMobile),我如何仅使用联系人名称过滤数据。

【问题讨论】:

    标签: angularjs angularjs-filter


    【解决方案1】:

    您可以使用对象而不是字符串作为键。 见:http://docs.angularjs.org/api/ng.filter:filter

    <input type="text" ng-model="nameFilter.contactName" placeholder="Enter a name here">
    

    【讨论】:

      猜你喜欢
      • 2016-03-18
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      相关资源
      最近更新 更多