120

Is there a way to *ngFor loop a defined number of times instead of always having to iterate over an array?

For example, I want a list to repeat 5 times, the loop would be something like that in C#;

for (int i = 0; i < 4; i++){

}

Desired result:

<ul>
   <li><span>1</span></li>
   <li><span>2</span></li>
   <li><span>3</span></li>
   <li><span>4</span></li>
   <li><span>5</span></li>
</ul>
Rodrigo Real
  • 2,993
  • 5
  • 14
  • 10
  • 1
    one method is as said by @TGH and second one you can use your for loop as mentioned in question then push value of [i] in empty array and start *ngFor on that array this is also possible. you can chose either one from these two way. – Pardeep Jain Dec 22 '15 at 05:12

1 Answers1

124

Within your component, you can define an array of number (ES6) as described below:

export class SampleComponent {
  constructor() {
    this.numbers = Array(5).fill(0).map((x,i)=>i);
  }
}

See this link for the array creation: Tersest way to create an array of integers from 1..20 in JavaScript.

You can then iterate over this array with ngFor:

@View({
  template: `
    <ul>
      <li *ngFor="let number of numbers">{{number}}</li>
    </ul>
  `
})
export class SampleComponent {
  (...)
}

Or shortly:

@View({
  template: `
    <ul>
      <li *ngFor="let number of [0,1,2,3,4]">{{number}}</li>
    </ul>
  `
})
export class SampleComponent {
  (...)
}

Hope it helps you, Thierry

Edit: Fixed the fill statement and template syntax.

Chris Weber
  • 5,555
  • 8
  • 44
  • 52
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Mitja, that you phrased it like that makes me happy. I needed a laugh. It is kind of pathetic, isn't it?? – Scott Jan 20 '19 at 03:20
  • As most of cases typescript (or linter) may argue about `x` variable, asking to remove it because it's not using. To fix that you need to write: `Array(columnAmount).fill(0).map(({}, i) => i)` – Arsen Khachaturyan Jan 20 '19 at 11:18
  • @Mitja that this is still the only solution makes me really sad. – alisonmsmith Mar 28 '19 at 16:06
  • 40
  • @olNoy this should be an answer, It is currently hidden behind "Show 4 more comments". – Stephen Jul 07 '20 at 20:52
  • @olNoy's solution should be the right answer IMO. – adamsfamily Aug 12 '20 at 12:13
  • 27
    To get exactly what the OP wants (each li containing a sequential number starting at 1), extend @olNoy's answer: ```
  • {{i+1}}
  • ``` – Daniel Flippance Oct 07 '20 at 00:06
  • Using DanielFlippance's example I was able to achieve the span repetition using only `*ngFor="let item of [].constructor(5);"`. I didn't need the `i` incrementor because all my spans had the same content. – KC Coder Jun 23 '21 at 22:01
  • 1
    @DanielFlippance why haven't you posted the answer instead of a comment. – MeVimalkumar Aug 26 '21 at 12:59
  • 1
    @MeVimalkumar This question appears only allow comments at this time – Daniel Flippance Aug 26 '21 at 22:10
  • creating a dummy Array for looping seems like a waste of memory. – hadaytullah Jan 21 '22 at 11:17
  • Do not call the array constructor within the html binding, this is a much worse waste of time for angular than just defining it in the cs. If you bind anything to a method, that method will be re-called every time any data changes, because angular has no way of knowing which data change will effect that binding. If it's bound to a variable in the cs, then angular can correctly know when to react to data changes. – claudekennilol Jan 09 '23 at 17:56