【问题标题】:Create Row every after 2 item in Angular ng-repeat - Ionic Grid在Angular ng-repeat中的每2个项目之后创建行 - Ionic Grid
【发布时间】:2019-11-25 21:51:07
【问题描述】:

我需要通过 ng-repeat 在我的应用中创建如下结构。

<div class="row">
  <div class="col-50">1</div>
  <div class="col-50">2</div>
</div>
<div class="row">
  <div class="col-50">3</div>
  <div class="col-50">4</div>
</div>

现在我的代码如下:

<div class="row">
  <label class="item item-radio col col-50" ng-repeat="a in question.answer">
  <input type="radio" name="answers"   class="a-{{ a.correct }}" value="{{ a.correct }}" ng-click='ansValue("{{ a.correct }}")'> 

  <div class="item-content">
     <img src="img/ans/{{ a.option }}" />
  </div>

  <i class="radio-icon ion-checkmark"></i>
  </label>
</div>

但在上面的代码中,它只是我拥有的一个单行标签。我不想以某种方式让行标签包含/包装循环中的每 2 个项目。实现这一目标的最佳方法是什么?

参考:Ionic Grid Doc

【问题讨论】:

  • 阅读 ng-repeat-start 和 ng-repeat-end。
  • 你好@wayne,谢谢。虽然这并没有解决我的问题。我从来不知道这存在。下面的答案似乎是我现在的解决方案。

标签: angularjs grid angularjs-ng-repeat ionic-framework


【解决方案1】:

我设法使用$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 一个工作的 JSFiddle。

【讨论】:

  • 我正在这样做,但是 %3 在 ng-repeat 中有很多标记。我发现我可以像这样执行嵌入式 ng-repeat,而不是重复我的代码 3 次: ng-repeat="i in [$index+0,$index+1,$index+2]" 然后我只需要做一次我的标记并参考 list_of_items[i]
【解决方案2】:

@Patrick Reck 的解决方案非常好,但它会迫使您重复两次代码,

我建议改进:

    <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>

这样你就可以像普通的 ng-repeat 一样编写一次代码

【讨论】:

  • 这很有效,但前提是你有偶数数量的数字。否则,它会留下一个尾随 div。我不得不在 .col.col-50 周围添加另一个包装 div 和 ng-if="number" 以忽略尾随的空数字。
  • @joshwhatk 简单的解决方案是以这种方式添加 ng-if:&lt;div class="col col-50" ng-repeat="num in [numbers[$index],numbers[$index + 1]]" ng-if="num"&gt;
  • @ET-CS 你是绝对正确的,直接添加ng-if 更简洁。我无法编辑评论以添加它,谢谢!
【解决方案3】:

你可以添加 flex-wrap: wrap 到类行

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];
}

【讨论】:

  • flex-wrao: wrap 不适用于我的 4.1.1 android 设备。你能帮我吗?
  • 测试:flex-flow: row wrap
【解决方案4】:

我会这样写,你可以增加$index % 3来匹配你想要的列数,这个会创建3列...

<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>

【讨论】:

  • 对于 Ionic4,标签为 &lt;ion-row&gt;&lt;ion-col&gt; 只需设置 size="x" - 当列数达到最大值 12 - 行数将下降。但是没有size 属性-它们都将被挤在一行中...
【解决方案5】:

此解决方案使用 flex-flow 并出色地解决了这个问题:

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>

【讨论】:

    【解决方案6】:

    尝试如下。您可以使用以下代码创建任意数量的列。您只需要使用离子网格的 size 属性并将 noOfColumns 替换为您想要的列数。在这种情况下,只需将 2 用于 noOfColumns。它会像魅力一样发挥作用。

    角网格基于 12 列布局,根据屏幕大小具有不同的断点。参考:https://ionicframework.com/docs/layout/grid

    <ion-grid>
        <ion-row >      
             <ion-col ng-repeat="n in numbers" size="{{12/noOfColumns}}">                   
                    {{ n }}
             </ion-col> 
        </ion-row>  
    </ion-grid>
    

    【讨论】:

    • 2014 年提出的问题,您现在正在添加答案,为什么?
    • 现在只学习 Ionic 和 Angular。这可能对其他人有用。
    猜你喜欢
    • 2016-09-12
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    相关资源
    最近更新 更多