AngularJS limitTo:4计数不应显示的元素

时间:2017-04-21 13:37:08

标签: javascript angularjs json angularjs-ng-repeat filtering

我的过滤器出了问题。我有一个JSON数据,我想在我的网站上显示4个过滤的元素。它看起来像这样:

<div ng-repeat="place in places | limitTo: 4 | filter: {cheap: 'true'}" class="col-lg-3">

所以,我想只显示“cheap = true”时的元素。我的问题是来自JSON的第一个元素有“cheap = false”而我的ng-repeat只显示3个项目而不是4个。我认为它从数据的开头算起并以4个元素结束,所以如果元素1便宜是假的只显示接下来的3个是真的。

我怎样才能做出类似“如果元素1便宜是假的,跳过它并显示接下来的4个元素,这是真的”。

我希望你理解我的意思。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

根据评论的解决方案,它很容易 - 我不得不切换订单和地点&#34;过滤器:&#34;首先,比限制:&#34;。谢谢!

答案 1 :(得分:0)

只需将limitTo放在filter

之后

var myApp = angular.module('myApp', []);


function MyCtrl($scope) {
  $scope.places = [{
    cheap: true
  }, {
    cheap: true
  }, {
    cheap: false
  }, {
    cheap: true
  }, {
    cheap: true
  }, {
    cheap: true
  }, {
    cheap: true
  }, ]
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="MyCtrl">
    <h1>Good</h1>
    <div ng-repeat="place in places  | filter: {cheap: 'true'} | limitTo: 4" class="col-lg-3">
      {{place.cheap}}
    </div>

    <h1>Bad</h1>
    <div ng-repeat="place in places| limitTo: 4  | filter: {cheap: 'true'} " class="col-lg-3">
      {{place.cheap}}
    </div>
  </div>
</div>