Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular: how to loop for numbers?

Tags:

angular

ngfor

I have a collection of items, where each item have a rank attribute, which is a number. I wan to loop over this number, here is what I've tried:

<div class="col-md-3 col-sm-4 item-container" *ngFor="let item of items">     <div class="item">         <p class="rank">             <img src="" class="img-responsive" *ngFor="let rank of item.rank"/>         </p>     </div> </div> 

typescript:

export class MonthpicksComponent{      items: Item[] = [];     //... }  export class Item{   id: number;   name: string;   img: string;   desc: string;   rank: number;   voters: number; } 

but unlucky the first loop shows only one result and the second loop showed nothing.

like image 799
Mohammad Avatar asked Oct 18 '17 07:10

Mohammad


People also ask

Can we use for loop in angular?

The for–in loop is for looping over object properties. The for–of loop is for looping over the values in an array. for–of is not just for arrays. It also works on most array-like objects including the new Set and Map types which we will cover in the next lecture.

How do you iterate numbers in HTML?

Approach 1: Using the for loop: The HTML elements can be iterated by using the regular JavaScript for loop. The number of elements to be iterated can be found using the length property. The for loop has three parts, initialization, condition expression, and increment/decrement expression.

What is * ngFor?

*ngFor is a predefined directive in Angular. It accepts an array to iterate data over atemplate to replicate the template with different data. It's the same as the forEach() method in JavaScript, which also iterates over an array.


1 Answers

Maybe the simplest solution without any TS code:

<div *ngFor="let item of [].constructor(10); let i = index">    {{i}} </div> 
like image 88
NJoco Avatar answered Oct 06 '22 19:10

NJoco