0

This question is asked by my friend. Just want to know, Could we define *ngFor without defining array in the component for eg. minValues?

  <select>
              <div *ngFor="let value of minValues; let i=index">
                  <option class="min-comp form-control">{{index}}</option>
              </div>
  </select>

minValues = [1,2,3,4, ....... 100] or [1,2,3 ...... 200] or [1,2,3 .... n] // here n would any constant value

I'm thinking, we can't but not sure...

Why do we need to go to component if we just want to repeat *ngFor for n number of times, Can not we do this in a template with clear syntax like repeat ngFor from 1 to 100?

If this question has been asked in the interview what would you say?

Thanks!

Sandeep Sharma
  • 1,855
  • 3
  • 19
  • 34

2 Answers2

4

Try this:

<div *ngFor="let dummy of ' '.repeat(20).split(''), let x = index">Test</div>
Sunil
  • 411
  • 5
  • 12
0

Check this out:

Angular 2 - NgFor using numbers instead collections

shortly:

@Component({
  template: `
<ul>
  <li *ngFor="let number of [0,1,2,3,4]">{{number}}</li>
</ul>
`
})
export class SampleComponent {
 (...)
}
Nix
  • 31
  • 1
  • 6