searchKeyword not working in AngularJS filter

℡╲_俬逩灬. 提交于 2019-12-12 00:30:02

问题


I am writing an app using AngularJS on the front end. I want to search through a table by any word/field; one search box for everything. According to this article here: http://hello-angularjs.appspot.com/searchtable I can use filter: searchKeyword to do just this.

This is my code, and it is not working as expected.

<label>Search: <input ng-model="searchKeyword"></label>

<table ng-repeat="post in posts | orderBy: sort | filter: searchKeyword">
    <tr>
       <td> {{index(post)}} </td>

       <td> {{post.title}} </td>

       <td> {{post.author}} </td>

       <td> {{post.content}} </td>

       <td> {{post.date | date: "d/M/yyyy"}} </td>
    </tr>
</table>

What should I do? Thank you


回答1:


Try this:

Controller

    $scope.posts = [
      {title:'Title one', author:'Author one', content: 'Content one'},
      {title:'Title two', author:'Author two', content: 'Content two'},
      {title:'Title three', author:'Author three', content: 'Content three'}
    ];

    $scope.searchKeyword = '';
    $scope.sort = 'title';

View

<label>Search:
  <input ng-model="searchKeyword">
</label>

<table ng-repeat="post in posts | orderBy: sort | filter: searchKeyword">
  <tr>
    <td> {{post.title}} </td>
    <td> {{post.author}} </td>
    <td> {{post.content}} </td>
  </tr>
</table>



回答2:


I ran into a similar issue myself.

I did something like this

<input class="form-control" type="text" ng-model="searchFilter.itemDesc" 
    placeholder="Search...." />

<tr ng-repeat="item in items | filter: searchFilter">

each item has a property of itemDesc

the downside to this approach is that it only lets you search one property of the object being repeated

If you are wanting it to be searched by the different properties of the object

you can do something similar to

Filtering by Multiple Specific Model Properties in AngularJS (in OR relationship)



来源:https://stackoverflow.com/questions/31393712/searchkeyword-not-working-in-angularjs-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!