2

I know the best way is to define a variable from the .ts. But in this case I require to be able to do an *ngFor from a given number, where this number will be an array with this number of positions.

My idea is something like this:

<ng-container * ngFor = "let item of new Array(number)>
  ...
</ng-container>

for example: number =3

<ng-container * ngFor = "let item of new Array(3)>
  repetat 3 times
</ng-container>

is it possible?

yavg
  • 2,761
  • 7
  • 45
  • 115

1 Answers1

1

Here is one way, using a string. An initial string 'x' is repeated the desired number of times. The array is then obtained by splitting the string on each character.

<ng-container *ngFor="let item of 'x'.repeat(count).split('')">
  ...
</ng-container>

See this stackblitz for a demo.

ConnorsFan
  • 70,558
  • 13
  • 122
  • 146