yavg
yavg

Reputation: 3051

how to define an array in template (not use .ts)

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?

Upvotes: 2

Views: 396

Answers (1)

Martin Parenteau
Martin Parenteau

Reputation: 73731

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.

Upvotes: 2

Related Questions