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.

2 Answers 2

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

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

1 Comment

his answer is what I wanted, I understand that it is not possible to use without creating an array
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.

1 Comment

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.