Angular 2: How to write a for loop, not a foreach loop

后端 未结 9 492
甜味超标
甜味超标 2020-12-05 09:41

Using Angular 2, I want to duplicate a line in a template multiple times. Iterating over an object is easy, *ngFor="let object of objects". However, I

9条回答
  •  -上瘾入骨i
    2020-12-05 09:57

    You can instantiate an empty array with a given number of entries if you pass an int to the Array constructor and then iterate over it via ngFor.

    In your component code :

    export class ForLoop {
      fakeArray = new Array(12);
    }
    

    In your template :

    • Something {{ index }}

    The index properties give you the iteration number.

    Live version

提交回复
热议问题