2016-04-10 156 views
47

如何使用*ngFor多次重複HTML元素?使用基於數字的ngFor多次重複HTML元素

例如: 如果我有一個成員變量分配爲20.我如何使用* ngFor指令使div重複20次?

+1

也見https://stackoverflow.com/questions/47586525/parse-array-in更換20 -angular-template/47586692#47586692 – yurzui

回答

49

您可以使用下列內容:

@Component({ 
    (...) 
    template: ` 
    <div *ngFor="let i of Arr(num).fill(1)"></div> 
    ` 
}) 
export class SomeComponent { 
    Arr = Array; //Array type captured in a variable 
    num:number = 20; 
} 

或實現自定義管道:

@Pipe({ 
    name: 'fill' 
}) 
export class FillPipe implements PipeTransform { 
    transform(value) { 
    return (new Array(value)).fill(1); 
    } 
} 

@Component({ 
    (...) 
    template: ` 
    <div *ngFor="let i of num | fill"></div> 
    `, 
    pipes: [ FillPipe ] 
}) 
export class SomeComponent { 
    arr:Array; 
    num:number = 20; 
} 
+1

FillPipe類必須實現PipeTransform – toumir

+1

是的,你是對的!它更好;-)感謝您指出這一點。我相應地更新了我的答案... –

+0

我不知道我將不得不創建自定義管道。以爲它會被內置到角度2,但你的建議完美地起作用。謝謝! – takeradi

15

您可以在HTML簡單的做到這一點:

*ngFor="let number of [0,1,2,3,4,5...,18,19]" 

,並使用變量「數字」來索引。

+0

OP表示他將'20'分配給一個成員變量..所以這不會有太大幫助 – Blauhirn

0

更簡單的方法:

定義helperArray和動態實例化它(或者,如果你想靜態)與您要創建的HTML元素的計數的長度。例如,我想從服務器獲取一些數據,並使用返回的數組長度創建元素。

export class AppComponent { 
    helperArray: Array<any>; 

    constructor(private ss: StatusService) { 
    } 

    ngOnInit(): void { 
    this.ss.getStatusData().subscribe((status: Status[]) => { 
     this.helperArray = new Array(status.length); 
    }); 
    } 
} 

然後在我的HTML模板中使用helperArray。

<div class="content-container" *ngFor="let i of helperArray"> 
    <general-information></general-information> 
    <textfields></textfields> 
</div> 
12

有兩個問題與使用Arrays建議的解決方法:

  1. 這是一種浪費。特別是對於大數量。
  2. 你必須將它們定義在某個地方,這會導致很多雜亂的操作。

似乎更有效,以限定Pipe(一次),並返回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; 
    } 
} 

使用實例(呈現與動態寬度的網格/高度):

<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> 
+1

這非常有用。 – Mordred

+0

正是我在找的東西。而且它在你的介紹中提到的效率也很高。 –

5

甲更簡單並且可重用的解決方案可以使用像這樣的自定義結構指令。

import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; 

@Directive({ 
    selector: '[appTimes]' 
}) 
export class AppTimesDirective { 

    constructor(
    private templateRef: TemplateRef<any>, 
    private viewContainer: ViewContainerRef) { } 

    @Input() set appTimes(times: number) { 
    for (let i = 0 ; i < times ; i++) { 
     this.viewContainer.createEmbeddedView(this.templateRef); 
    } 
    } 

} 

而且使用這樣的:

<span *appTimes="3" class="fa fa-star"></span> 
+0

欲瞭解更多信息:https://netbasal.com/the-power-of-structural-directives-in-angular-bfe4d8c44fb1 –

6
<div *ngFor="let dummy of ' '.repeat(20).split(''), let x = index"> 

與可變

+0

我希望我可以標記正確的答案。 – hamzox