-3

I have a number and I want to iterate that number of times.

for (var _i = 0; _i < length; _i++)

I want to implement similar to this syntax in the html template. if i use ngFor then i will need to have a collection but I don't have collection, I just have length.

Alan Xalil
  • 1
  • 1
  • 6

1 Answers1

2

You could create an array of the right size in the component and iterate on it:

@Component({
  selector: 'app-root',
  template: `
    <ul>
      <li *ngFor="let i of arr">foo</li>
    </ul>
  `
})
export class AppComponent {
  arr = new Array(10);  // let's say 10 is your number
}

FYI, I tried declaring the array directly in the template but it doesn't work:

<ul>
  <li *ngFor="let i of new Array(10)">foo</li>
</ul>
AngularChef
  • 13,797
  • 8
  • 53
  • 69