Create a loop for, in angular 2 from a template

Maybe use angular2 in the template,

something like the following:
for int a = 0; a < numberTest; a++

this test number is in the code component:

numberTest: number = 5;

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

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

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

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

but my question is, if possible, create a for template that accepts a value condition using a component variable, hopefully I will explain well.

+4
source share
2 answers

Maybe something like this:

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

with auxiliary 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

+4
source

NgFor includes value indexfor this purpose

.

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

: Angualar2 Docs - NgFor

. , .

+3

Source: https://habr.com/ru/post/1629294/


All Articles