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?

1
  • Possible, yes, bad idea, likely yes Commented Apr 1, 2020 at 22:19

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.