Create Row every after 2 item in Angular ng-repeat - Ionic Grid

岁酱吖の 提交于 2019-11-28 09:38:22

I managed to do it using $even.

<div ng-repeat="number in numbers">

    <div class="row" ng-if="$even">
        <div class="col col-50">{{numbers[$index]}}</div>
        <div class="col col-50">{{numbers[$index + 1]}}</div>
    </div>

</div>

Here's a working JSFiddle.

The solution from @Patrick Reck is excellent, but it forces you to repeat your code twice,

I suggest this improvement:

    <div ng-repeat="number in numbers">    
        <div class="row" ng-if="$even">
            <div class="col col-50" 
                 ng-repeat="num in [numbers[$index],numbers[$index + 1]]">
                {{num}}
            </div>
        </div>
    </div>

this way you will write your code one time as if it is a normal ng-repeat

You can add flex-wrap: wrap to class row

http://jsfiddle.net/0momap0n/99/

<div ng-controller="MyCtrl">
    <div class="row" style="flex-wrap: wrap">
    <div class="col col-50" ng-repeat="number in numbers">{{number}}</div>
</div>  

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

function MyCtrl($scope) {
    $scope.numbers = [1, 2, 3, 4, 5, 6];
}

I would write it like this, you can increase the $index % 3 to match the number of columns that you would like, this one will create 3 columns ...

<div class="row">
   <div class="" ng-repeat="i in range(1, 9)">
     <div class="clearfix" ng-if="$index % 3 == 0"></div>

       <div class="col">  
         <h1>{{ i }}</h1>
       </div>

     </div>
</div>

This solution is using flex-flow and solves this brilliantly:

CSS

 .container {
    display: flex;
    flex-flow: row wrap;
  }
  .item {
    width: 50%;
  }

HTML

    <div class="container">
   <div class="item" *ngFor="let number of numbers">
      {{number}}
  </div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!