angular - 我们可以将html模板从一个组件传递到Angular2中的另一个组件吗?

标签 angular

假设我有一个可重复使用的基本组件 CardComponent,即它将接受诸如 1. 数据数组 2. HTML模板(迭代)

因此消费者组件将使用 CardComponent 选择器并传递 dataArray 和 Template。

我怎样才能实现它?(传递html模板)

最佳答案

是的,可以使用 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 而不是的更多详细信息

https://medium.com/michalcafe/angulars-content-projection-trap-and-why-you-should-consider-using-template-outlet-instead-cc3c4cad87c9

Angular 2 use a "template" for ng-content to use inside component loop

关于angular - 我们可以将html模板从一个组件传递到Angular2中的另一个组件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47550821/

相关文章:

javascript - 如何使用 TypeScript/Angular 自定义我的 XLSX?

angular - FormGroup CustomFilter 中的 Mat-AutoComplete

angular - 在 angular2 中实现向导?

angular - 单击 p-dialog close(X) 按钮时如何调用 Angular 函数?

angular - 将鼠标事件传递给组件 onclick

检测到 Angular 4 循环依赖

css - 将侧边栏组件放在 div 中

javascript - Angular : Adding the result of Http response to a map

angular - 可以为 Angular Material 表指定行标题吗?

angular - 错误类型错误 : _co. onBlueprintAdded 不是函数