【问题标题】:How to use the "for" loop statement in an angular html template?如何在角度 html 模板中使用“for”循环语句?
【发布时间】:2019-07-02 06:00:19
【问题描述】:

我在组件中定义了一个“组”变量。当 group 的值为 3 时,我在 html 模板中创建了 3 列。详情如下:

class A{
    group = 3;
}

<th>first th</th>
<th>second th</th>
<th>third th</th>

如何在html模板中实现这个功能?

【问题讨论】:

标签: angular typescript


【解决方案1】:

试试这样:

 <th *ngFor="let item of fakeArray(3);">...</th>

TS:

 fakeArray(length: number): Array<any> {
    if (length >= 0) {
      return new Array(length);
    }
  }

【讨论】:

    【解决方案2】:

    Angular 标记语言没有倒计时的“for”循环。只需创建数组字段并通过 ngFor 对其进行迭代。

    class A{
        columns = ['first', 'second', 'third'];
    }
    
    
    <th *ngFor="let c of columns">{{c}} th</th>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 2017-11-17
      • 1970-01-01
      • 2019-04-27
      • 2019-05-28
      相关资源
      最近更新 更多