回答
您可以使用下列內容:
@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;
}
您可以在HTML簡單的做到這一點:
*ngFor="let number of [0,1,2,3,4,5...,18,19]"
,並使用變量「數字」來索引。
OP表示他將'20'分配給一個成員變量..所以這不會有太大幫助 – Blauhirn
更簡單的方法:
定義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>
有兩個問題與使用Arrays
建議的解決方法:
- 這是一種浪費。特別是對於大數量。
- 你必須將它們定義在某個地方,這會導致很多雜亂的操作。
似乎更有效,以限定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>
這非常有用。 – Mordred
正是我在找的東西。而且它在你的介紹中提到的效率也很高。 –
甲更簡單並且可重用的解決方案可以使用像這樣的自定義結構指令。
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>
欲瞭解更多信息:https://netbasal.com/the-power-of-structural-directives-in-angular-bfe4d8c44fb1 –
- 1. 使用XSL連接基於多個元素的重複元素信息
- 2. JavaScript重複元素的計數次數
- 3. 伍重複一個元素多次
- 4. 重複的html元素
- 5. Matlab:重複字符串元素N次
- 6. 重複使用CSS樣式並應用於其他HTML元素
- 7. JavaScript複雜數組「減少元素」 - 基於數組元素構建HTML表格
- 8. 如何計算數組中重複元素的重複次數?
- 9. 重複div元素n次
- 10. 使用ng-repeat-start重複多次元素
- 11. 將類添加到基於數據的元素-html使用javascript
- 12. 元素按重複一定次數
- 13. 基於向量中的計數重複矩陣元素
- 14. 使用XSLT重複元素x的次數
- 15. 排序元素按字母順序重複基於子女
- 16. 元素在* ngFor
- 17. 重複在多個元素的函數
- 18. 如何使用基於給定數字的jquery注入html元素?
- 19. 基於數字重複字符
- 20. 多次用html元素替換單詞
- 21. ngFor每重複
- 22. 用於重複元素+唯一ID的HTML模板
- 23. 多次重複使用gtk_file_chooser_dialog
- 24. 如何使用重複元素在多行中組合元素
- 25. 使用JavaScript插入HTML元素兩次(或多次)
- 26. 使用XSLT連接非重複元素和重複元素
- 27. 顯示基於PHP值的HTML元素
- 28. 如何獲取XML元素的行數,在多重複雜和重複的層次上使用XSLT?
- 29. 角2 * ngFor重新渲染元素
- 30. 基於點擊的兩個重複類名稱的元素ID
也見https://stackoverflow.com/questions/47586525/parse-array-in更換
20
-angular-template/47586692#47586692 – yurzui