根据搜索输入过滤 ng-repeat [英] Filter ng-repeat based on search input

查看:29
本文介绍了根据搜索输入过滤 ng-repeat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 html:

 

<div><input type="text" placeholder="Search" ng-model="userSearch.firstname">

<div><div ng-repeat="user in users | filter:userSearch "><div>{{user.firstname}} {{user.lastname}}

所以我有一个输入字段,用户可以在其中键入和搜索我的所有用户列表.有没有一种方法可以根据用户在搜索输入中输入的内容在 ng-repeat 中应用内联过滤器?

当前解决方案仅过滤名字

我想过滤名字和姓氏

尝试过:

 

<div><input type="text" placeholder="Search" ng-model="userSearch">

<div><div ng-repeat="user in users | filter:{firstname: userSearch, lastname: userSearch} "><div>{{user.firstname}} {{user.lastname}}

但我认为这是一个 AND 而不是一个 or.

解决方案

将 ng-model 从 userSearch.firstname 更改为 userSearch.

 

http://jsbin.com/keyuno/1/edit?html,js,output

或者,您可以将 filter:userSearch 更改为 filter:userSearch.firstname.您只需要确保过滤器与模型匹配即可.

I have the following html:

  <div>
    <div>
      <input type="text" placeholder="Search" ng-model="userSearch.firstname">
    </div>
  </div>

 <div>
   <div ng-repeat="user in users | filter:userSearch ">
     <div>
       {{user.firstname}} {{user.lastname}}
     </div>
   </div>
 </div>

So I have an input field where the users can type and search in my list of all users. Is there a way I can apply a filter inline in the ng-repeat based on what the user types in the search input?

Current solution only filters on firstname

I would like to filter on both first and last name

Tried:

  <div>
    <div>
      <input type="text" placeholder="Search" ng-model="userSearch">
    </div>
  </div>

 <div>
   <div ng-repeat="user in users | filter:{firstname: userSearch, lastname: userSearch} ">
     <div>
       {{user.firstname}} {{user.lastname}}
     </div>
   </div>
 </div>

But I think that is an AND not an or.

解决方案

Change ng-model from userSearch.firstname to just userSearch.

 <input type="text" placeholder="Search" ng-model="userSearch">

http://jsbin.com/keyuno/1/edit?html,js,output

Alternatively you could change filter:userSearch to filter:userSearch.firstname. You just need to be sure the filter matches the model.

这篇关于根据搜索输入过滤 ng-repeat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆