-1

I defined a "group" variable in the component. When the value of group is 3, I create 3 columns in the html template. The details are as follows:

class A{
    group = 3;
}

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

How to implement this function in html template?

tanjie
  • 19
  • 1
  • 5
  • you can check the already existing same issue like https://stackoverflow.com/questions/34405878/way-to-ngfor-loop-defined-number-of-times-instead-of-repeating-over-array?rq=1 – Robin gupta Jul 02 '19 at 06:10

2 Answers2

0

Try like this:

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

TS:

 fakeArray(length: number): Array<any> {
    if (length >= 0) {
      return new Array(length);
    }
  }
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
0

Angular markup language doesn't have "for" cycle with countdown. Just make array field and iterate it one via ngFor.

class A{
    columns = ['first', 'second', 'third'];
}


<th *ngFor="let c of columns">{{c}} th</th>
Nail Achmedzhanov
  • 1,264
  • 1
  • 9
  • 12