假设我有一个可重用的基本组件CardComponent,即它将接受诸如1. DataArray 2. HTML模板(经过迭代)的输入。
因此,消费者组件将使用CardComponent选择器并传递dataArray和Template。
我该如何实现?(传递htmltemplate)
是的,可以使用ng-content方法传递HTML模板,但是您应该始终使用模板方法。
您的.html文件->通用->
<div>
<ng-container *ngTemplateOutlet="template"> </ng-container>
</div>
您的.ts文件
@ContentChild(TemplateRef)
template: TemplateRef<any>;
以及要传递模板的其他组件文件:
<my-component>
<ng-template> Your HTML Content </ng-template>
</my-component>
有关为什么应考虑使用ng-template而不是的更多详细信息
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句