#Need an array as long as a number in an ngFOr

10 messages · Page 1 of 1 (latest)

crystal tiger
#
<div id="Attacco" *ngFor="let i of moduli[index]">
  {{i}}
  <!--CREA ARRAY LUNGO QUANTO i-->
  <p *ngFor="let j of i.length???">
    <app-carta></app-carta>
  </p>
</div>
mellow patio
#

write a custom pipe that transforms a number to an iterable, then use pipe inside ngFor

stiff cape
#

a simple solution would be:

<p *ngFor="let j of [].constructor(i.length)">
  <app-carta></app-carta>
</p>

but what is i? a string? a number? an array?

mellow patio
#

or use generator function inside component

  protected *iterate(num: number) {
    for (let i = 0; i < num; i++) {
      yield i
    }
  }
crystal tiger
stiff cape
#

[].constructor(10) is the same as new Array(10) so it creates an array of 10 items but all empty.

then your *ngFor will iterate over the 10 empty elements

mellow patio
#

keep in mind, that it creates a new array every time change detection is run