Es gibt zwei Probleme mit den empfohlenen Lösungen mit Arrays
:
- Es ist verschwenderisch. Insbesondere für große Zahlen.
- Sie müssen sie irgendwo definieren, was viel Unordnung für solch eine einfache und allgemeine Operation ergibt.
Es scheint effizienter, ein Pipe
(einmal) zu definieren, Return ein Iterable
:
import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'times'})
export class TimesPipe implements PipeTransform {
transform(value: number): any {
const iterable = {};
iterable[Symbol.iterator] = function*() {
let n = 0;
while (n < value) {
yield ++n;
}
};
return iterable;
}
}
Anwendungsbeispiel (ein Gitter mit dynamischer Breite Rendering/Höhe):
<table>
<thead>
<tr>
<th *ngFor="let x of colCount|times">{{ x }}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let y of rowCount|times">
<th scope="row">{{ y }}</th>
<td *ngFor="let x of colCount|times">
<input type="checkbox" checked>
</td>
</tr>
</tbody>
</table>
Siehe auch https://stackoverflow.com/questions/47586525/parse-array-in -angular-template/47586692 # 47586692 – yurzui