[英]angular how to dynamically add a list of components
*ngFor
遍歷一個模型,然后顯示孩子根據您想要實現的功能,您需要在已經提到的注釋中,在 TS 文件中創建模型,該文件將在 HTML 模板中使用*ngFor
。 將此與子組件結合使用,您可以輕松添加這些組件,例如單擊按鈕
TS 父母
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
// specifies the input data
inputs = []
addInput() {
// Here you can specify the data
this.inputs.push({});
}
}
HTML 父級
<button (click)="addInput()">Add Input</button>
<app-child *ngFor="let input of inputs"></app-child>
HTML 子級
<p> Input: </p>
<input type="text">
@Input()
裝飾器。 如果您想顯示不同的表單或不同的元素,您可以在那里將input
的數據傳遞給孩子。供您參考: https ://stackblitz.com/edit/angular-hrdtcu
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.