【问题标题】:Angular/Ionic swap elements within *ngFor*ngFor 中的 Angular/Ionic 交换元素
【发布时间】:2023-03-15 05:30:02
【问题描述】:

我希望能够使用*ngFor 创建一个项目列表,如下所示..

文字 - 图片
图片 - 文字
文字 - 图片
图片 - 文字强>

  <ion-grid>
    <ion-row *ngFor="let item of items">
      <ion-col col-6 ">
        <ion-card ">
          <ion-card-content>{{item.name}}
          </ion-card-content>
        </ion-card>
      </ion-col>
      <ion-col col-6 >
        <ion-card>
          <img [src]="item.img"/>
        </ion-card>
      </ion-col>
    </ion-row>
  </ion-grid>

但是,我不确定如何在 for 循环中实现这一点。 有什么建议?谢谢

【问题讨论】:

    标签: html css angular ionic2 material-design


    【解决方案1】:

    当项目的索引是奇数或偶数时应用 flex-direction: row-reverse,您的选择。

        <!--HTML-->
        <!--Track the index of each item and apply reverse class if its even or odd (which ever you prefer, just change the comparator)-->
        <ion-grid>
            <ion-row *ngFor="let item of items; let i = index;" [class.reverse]="i%2 !== 0">
                <ion-col col-6>
                    <ion-card>
                        <ion-card-content>{{item.name}}
                        </ion-card-content>
                    </ion-card>
                </ion-col>
                <ion-col col-6>
                    <ion-card>
                        <img [src]="item.img" />
                    </ion-card>
                </ion-col>
            </ion-row>
        </ion-grid>
    
        <!--CSS-->
        .reverse { flex-direction: row-reverse; }
    

    【讨论】:

    • 谢谢,这似乎工作。但是,第二个索引交换但不是并排对齐,而是显示一个在另一个之上。有什么解决办法吗?
    • 玩转行反转和列反转
    • 我明白了,我只需要改用row-reverse;。非常感谢
    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 2021-02-04
    • 2018-10-16
    • 2019-11-02
    • 2021-09-10
    • 2018-07-16
    • 2019-05-04
    • 2017-08-15
    相关资源
    最近更新 更多