281

...for example...

<div class="month" *ngFor="#item of myCollection; #i = index">
...
</div>

Is possible to do something like...

<div class="month" *ngFor="#item of 10; #i = index">
...
</div>

...without appeal to a non elegant solution like:

<div class="month" *ngFor="#item of ['dummy','dummy','dummy','dummy','dummy',
'dummy','dummy','dummy']; #i = index">
...
</div>

?

Marco Jr
  • 6,496
  • 11
  • 47
  • 86
  • 8
    I have the same problem. Really upset one cannot do such simple things with angular 2. – albanx Jul 04 '16 at 15:53
  • 1
    Maybe this can be helpful: https://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-a-range-within-the-supp – Pizzicato Jun 05 '18 at 08:23

17 Answers17

290

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

export class SampleComponent {
  constructor() {
    this.numbers = Array(5).fill().map((x,i)=>i); // [0,1,2,3,4]
    this.numbers = Array(5).fill(4); // [4,4,4,4,4]
  }
}

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:

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

Or shortly:

@Component({
  template: `
    <ul>
      <li *ngFor="let number of [0,1,2,3,4]">{{number}}</li>
    </ul>
  `
})
export class SampleComponent {
  (...)
}
TheUnreal
  • 23,434
  • 46
  • 157
  • 277
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • 7
    Yeah, Thierry ! It's not your fault, indeed, but still on the same context :( It's not elegant at all. But since you are a very skilled A2 developer, I can assume that there is no better solution. It's sad ! – Marco Jr Apr 01 '16 at 10:49
  • In fact, there is nothing for this in Angular2 in the loop syntax. You need to leverage what JavaScript provides to build arrays. For example: `Array(5).fill(4)` to create `[4,4,4,4,4]` – Thierry Templier Apr 01 '16 at 11:00
  • 3
    PS: @View annotation has been removed in angular2 beta 10 and above. – Pardeep Jain Apr 01 '16 at 11:15
  • 25
    Using `Array.fill()` in Angular 2 Typescript produces the following error `Supplied parameters do not match any signature of call t arget.` — Checking the Array.prototype.fill docs, it says it requires 1 argument... https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/fill – Joshua Russell Mar 12 '17 at 23:11
  • An alternative would be to use this: `Array.apply(null, {length: 5}).map(Number.call, Number)`. The result is `[0,1,2,3,4]` – Laurent Jacquot Jun 09 '17 at 23:30
  • In Angular 2 TypeScript you can use: Array(32).fill(1, 1, 32).map((x, i) => i); // monthdays from1 to 31 – Axel Schmitz Oct 25 '17 at 06:08
  • 19
    `Array(5).fill(1).map((x, i) => i + 1); /*[1,2,3,4,5]*/` this resolves the error in TS – mshahbazm Mar 27 '18 at 15:06
  • Hi friend but if I want to use fill()map() and start from 0, because if I start limit 5 for exemplo its return to me 0,1,2,3,4 if I want 1,2,3,4,5 ? – Rafael Moura Aug 26 '18 at 19:13
  • I don't know why would you use fill, I feel like the most readable alternative is `Array.from({ length: N });` which in addition accept an optional second parameter that is a map function. example: for creating an array of numbers you would simply `Array.from({length: N}, (_,k) => k )` which i find even more readable other than simpler, but This is my opinion. – Valex Jul 25 '19 at 14:28
  • Can I use `
  • {{tag}}
  • ` where `discussion.tags` is an `array` of tags. I am using typescript. – Tanzeel Sep 14 '19 at 04:25
  • Had to use parseInt because my number was a string & had to add +1 at the end because I wanted to iterate from 1, not 0. Array(parseInt(this.product.size_1_maxqty)).fill().map((x,i)=>i+1) – Grant Jul 17 '20 at 03:47
  • Above does a tiny bit of extra work (fills with undefined) but is relatively minor vis-a-vis the speedup you can achieve by using a for loop, and if you forget the .fill you may be confused why your array is mysteriously [empty x 5]. You can encapsulate the above as a custom function, or alternatively use a somewhat more intended method: ```Array.from(Array(5),(x,i)=>i)``` – Muhammad Abdullah Oct 13 '22 at 07:42