微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 ngFor 内的项目之间添加元素

如何解决如何在 ngFor 内的项目之间添加元素

我有这个模板,我可以在其中循环遍历项目并创建一个按钮:

<div *ngFor="let button of buttons">
  <p>
    <button
      type="button"
      mat-raised-button
      color="primary"
    >
      {{ button.text }}
    </button>
  </p>
</div>

enter image description here

我想在按钮之间添加一个文本。

enter image description here

该列表可以包含 2 个以上的按钮,因此我希望在所有按钮之间显示相同的文本。我希望这个文本按照图片在按钮之间居中。

解决方法

如果不是最后一个按钮,请将文本插入中继器:

<div *ngFor="let button of buttons; let last=last">
  <p>
    <button
      type="button"
      mat-raised-button
      color="primary"
    >
      {{ button.text }}
    </button>
  </p>
  <div *ngIf="!last">
    Text
  </div>
</div>
,

好吧,您可以简单地获取 *ngFor 的索引,例如 "*ngFor="let button of buttons;let i = index"" 然后

<div *ngFor="let button of buttons; let i = index">
  <p>
 
    <button
      type="button"
      mat-raised-button
      color="primary"
    >
      {{ button.text }}
    </button>
  </p>
       <div *ngIf="i== 2">this will show after the second iteration of the
  *ngFor</div>

</div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。