首页 > 解决方案 > 以编程方式将 PrimeNG 元素添加到 DOM

问题描述

亲爱的,就像在 JQuery 中我们使用 #div.append('') 或 #div.append('......') 一样,是否可以通过编程方式添加 PrimeNG 元素,如输入文本、输入数字、下拉列表、多选使用一些 json 格式的 api 或其他什么?

提前致谢

标签: angularprimeng

解决方案


由于您已将 angular 添加为标签..

只需在您的 html 中使用 *ngFor,然后对于您放置在数组中的每个 Json 对象,您都可以生成您想要的任何元素,例如:

items: MyItem[];
<app-custom-component *ngFor="let item of items" [myItem]="item"></app-custom-component>

customComponent 像这样处理它:

@Input() myItem: MyItem;
<h1>{{myItem.title}}</h1>
<input [(ngModel)]="myItem.description" />

等等...

或者一个非常基本的例子:

items = ['one', 'two', 'three'];
<h2 *ngFor="let item of items">{{item}}</h2>

推荐阅读