【问题标题】:How to create new row using ngFor [duplicate]如何使用 ngFor 创建新行 [重复]
【发布时间】:2018-07-06 11:48:13
【问题描述】:

我正在尝试使用 *ngFor:<div class="row"> <div class="col-lg-3" *ngFor="let user of users">{{user.username}}</div> </div> 创建新行

然后,如果 col div`s > 4,我需要创建新行。 我该怎么做?

【问题讨论】:

  • 您使用的是哪个引导程序?你能展示一个工作样本吗?如果它的引导程序 4,它应该自动将第 4 个 div 移动到下一行
  • 我正在使用引导程序 4。
  • 那么如果第4个元素来了就不需要创建新行,它应该自动换行
  • 它不能自动工作:( Bootstrap 将下一个 4 div 移动到页面下方,但如果我创建新行它可以正常工作。
  • 你能展示一个不工作的样本吗?

标签: angular bootstrap-4


【解决方案1】:

如果要使用 ngFor 创建行类的 div 元素,则将指令放入该 div 中。

ngFor 将添加内容包括写入它的 div

所以在你的例子中,它将是

HTML

<div class="row" *ngFor="let row of rows;let i = index">
   <div class="col" *ngFor="let col of row;let j = index">
     Row {{i}} Col {{ j }}
   </div>
</div>

TS

export class AppComponent  {
  public rows = [
    [1,1,1,1],
    [1,1],
    [1,1,1]
  ]
}

工作示例:stackblitz.com

【讨论】:

  • 不,我需要这样的东西:1t 行 -> 4 列; 2d 行 -> 4 列; 3d 行 -> 4 列
  • 每行的 4 列是恒定的或动态的,例如第一行-> 4 列,第二行-> 3 列...等@ВладМайлз
  • 检查我的新编辑@ВладМайлз
猜你喜欢
  • 2017-09-30
  • 2018-11-17
  • 2016-12-30
  • 2018-02-09
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
  • 2023-03-07
相关资源
最近更新 更多