[英]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.