Using ng-repeat and limitTo to limit the number of visible items displayed

后端 未结 4 1803
生来不讨喜
生来不讨喜 2020-12-09 14:48

I\'m trying to limit my result sets to a fixed number. I can use limitTo with ng-repeat, but this limits items regardless of their current visibili

相关标签:
4条回答
  • 2020-12-09 14:55

    There are a couple of approaches, maybe the most reusable is for you to create your own 'visible' custom filter which looks for visible attribute on your items. Then you could chain them.

    <div ng-repeat="item in items | visible | limitTo: 50">
    

    Here's a SO link to create custom filters

    0 讨论(0)
  • 2020-12-09 14:56

    You can use:

    <div ng-repeat="item in items | filter:{visible: true} | limitTo: 50">
        <p>{{item.id}}</p>
    </div>
    

    filter:{visible: true} will return a list of all visible items

    You can take a look at the angularjs docu for more information on the filter filter. http://docs.angularjs.org/api/ng.filter:filter

    0 讨论(0)
  • 2020-12-09 15:17

    It is also possible to do it this way:

    <div ng-repeat="item in items" ng-show="$index<50">
        <p>item.id</p>
    </div>
    
    0 讨论(0)
  • 2020-12-09 15:17

    You can use ng-if with $index to specify DOM display. Still generates ng-if comments but doesn't load the object so much improved performance noticed.

    <div ng-init="your.objects={{},{},{}}; your.max=10">
      <div ng-repeat="object in your.objects" ng-if="$index<your.max">
        {{object}}
      </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题