0

我是 AngularJS 的新手。我正在尝试根据用户名进行简单搜索,在我的情况下,该用户名未存储在最顶层。这是示例:

<div ng-init="users = [
{'fields':{'name':'Mary Brown','person_nickname':'M'},'username':'mbrown'},
{'fields':{'name':'John Smith','person_nickname':'J-Dog'},'username':'jsmith'}]">
</div>

这是搜索 我想要获取的所有字段并使其仅搜索的 plnkr fields.name。在这个 plunker 中,我可以输入“dog”并且仍然返回第二个用户,这不是我想要的。

我尝试将输入 ng-model 更改为,search.fields.name但是当我这样做时,搜索根本不会激活。我可以在文本框中输入任何字符串,并且没有任何内容被过滤掉。

我错过了什么?谢谢!

编辑:完整代码,因为 plunker 目前已关闭:

<!doctype html>
<html ng-app="App">
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js"></script>
    <script type="text/javascript" src="script.js"></script>
  </head>

  <body>

    <div ng-init="users = [
      {'fields':{'name':'Mary Brown','person_nickname':'M'},'username':'mbrown'},
      {'fields':{'name':'John Smith','person_nickname':'J-Dog'},'username':'jsmith'}]">
    </div>    

    Name only <input ng-model="search.fields"><br>

    <table id="userTable">
      <tr ng-repeat="user in users | filter:search">
        <td>{{user}}</td>
      </tr>
    </table>    

  </body>
</html>
4

1 回答 1

0

Plnkr is not running right now, but if you have an ng-repeat which you want to filter, then look at the official docs, especially upvoted comments are useful: http://docs.angularjs.org/api/ng.filter:filter

item in myArray | filter:{propertyThatWillBeFiltered: value}

If you are talking about searching inside your model, then you should use "filter" method on an array.

于 2013-10-29T15:09:46.470 回答