5

Is possible, use something like the following in a template of angular2,

for int a = 0; a < numberTest; a++

this numberTest is in code component:

numberTest: number = 5;

and the template, something like this, it is better to illustrate what I mean:

<li *ngFor="#int a = 0; a < numberTest">

I know that could be solved using an array, and iterate for example:

<li *ngFor="#item of items">

but my question is, if possible, create a for, in the template, which take the value condition using a variable component, hope I explain well.

Angel Angel
  • 19,670
  • 29
  • 79
  • 105

2 Answers2

4

Maybe something like this:

<li *ngFor="#int of range(numberTest)">

with a helper function:

let range = (value) => { 
 let a = []; for(let i = 0; i < value; ++i) { a.push(i+1) } return a; }

http://plnkr.co/edit/CNVqxU?p=preview

Sasxa
  • 40,334
  • 16
  • 88
  • 102
3

NgFor includes an index value for exactly this purpose

No function or intermediate state necessary.

<ul *ngFor="#item of items; #i = index">
  <li *ngIf="i < numberTest">{{ item }}</li>
</ul>

Source: Angualar2 Docs - NgFor

Note: This isn't tested so it may require a little tweaking.

Evan Plaice
  • 13,944
  • 6
  • 76
  • 94