【问题标题】:limitTo angularjs with more complex expression?limitTo 具有更复杂表达式的 angularjs?
【发布时间】:2014-05-07 06:42:59
【问题描述】:

我想显示我的ng-repeat 的有限结果,所以我首先想到的是limitTo。但是它的表达式 limitTo: 5 似乎没有解决我的问题。

我希望我可以用 limitTo 做更复杂的事情,例如。某些组的结果显示限制,例如仅显示来自美国的 50 辆汽车。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    您可以在 ng-repeat 中使用多个过滤器。

    这里是Fiddle。

    <div ng-repeat="people in peoples | filter:{country:'usa'} | limitTo:3">
                {{people.name}}
          </div>
    

    【讨论】:

      【解决方案2】:

      在你的 ng-repeat 中使用 :filter

      <div ng-init="friends = [{name:'John', phone:'555-1276'},
                               {name:'Mary', phone:'800-BIG-MARY'},
                               {name:'Mike', phone:'555-4321'},
                               {name:'Adam', phone:'555-5678'},
                               {name:'Julie', phone:'555-8765'},
                               {name:'Juliette', phone:'555-5678'}]"></div>
      
      Search: <input ng-model="searchText">
      <table id="searchTextResults">
        <tr><th>Name</th><th>Phone</th></tr>
        <tr ng-repeat="friend in friends | filter:searchText">
          <td>{{friend.name}}</td>
          <td>{{friend.phone}}</td>
        </tr>
      </table>
      <hr>
      Any: <input ng-model="search.$"> <br>
      Name only <input ng-model="search.name"><br>
      Phone only <input ng-model="search.phone"><br>
      Equality <input type="checkbox" ng-model="strict"><br>
      <table id="searchObjResults">
        <tr><th>Name</th><th>Phone</th></tr>
        <tr ng-repeat="friendObj in friends | filter:search:strict">
          <td>{{friendObj.name}}</td>
          <td>{{friendObj.phone}}</td>
        </tr>
      </table>
      

      Reference

      【讨论】:

      • 所以我应该编写自己的自定义过滤器?
      • 过滤器可以限制 ng-repeat 中的结果数量吗?他们既不把它全部隐藏,也不把它全部展示出来?
      猜你喜欢
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多