如何使用ng-repeat对2维json对象应用滤镜?

时间:2014-07-31 09:26:04

标签: javascript angularjs angularjs-ng-repeat angularjs-filter

这是我的json:

[{"node":
    {'name': 'Nexus S',
     'snippet': 'Fast just got faster with Nexus S.',
     'age': 1}},
    {"node":{'name': 'Motorola XOOM™ with Wi-Fi',
     'snippet': 'The Next, Next Generation tablet.',
     'age': 2}},
    {"node":{'name': 'MOTOROLA XOOM™',
     'snippet': 'The Next, Next Generation tablet.',
     'age': 3}}
  ]; 

这是我的代码:

Search-title: <input type='text' ng-model="name">
    <div class='cat' ng-repeat="(key,value) in phones ">
      <ul ng-repeat="fin in value | filter:name ">
        <li><h3>{{fin.name}}</h3>
        {{fin.snippet}}
        </li>
      </ul>
    </div>

如何为此json对象应用过滤器?

2 个答案:

答案 0 :(得分:1)

请看到此处:http://jsbin.com/kovegi/1/edit?html,js,output CodingNinja已关闭,只需使用ng-model="searchObj.node.name"代替ng-model="searchObj.name"

<div ng-controller="firstCtrl">
Search-title: <input type='text' ng-model="searchObj.node.name">
    <div class='cat' ng-repeat="(key,value) in phones  | filter:searchObj ">

  <ul ng-repeat="fin in value">
    <li><h3>{{fin.name}}</h3>
    {{fin.snippet}}
    </li>
  </ul>
</div>

答案 1 :(得分:0)

不是直接在过滤器中使用模型,而是将其设为对象并将值设置在同一变量中,然后在搜索中使用它

Search-title: <input type='text' ng-model="searchObj.node">
<div class='cat' ng-repeat="(key,value) in phones | filter:searchObj">
  <ul ng-repeat="fin in value">
    <li><h3>{{fin.name}}</h3>
    {{fin.snippet}}
    </li>
  </ul>
</div>