简体   繁体   English

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

[英]Can we pass html template from one component to another component in angular2?

Suppose i have a base component CardComponent which is resuable ie, it will accept input such as 1. DataArray 2. HTML template(that is iterated over) 假设我有一个可重用的基本组件CardComponent,即它将接受诸如1. DataArray 2. HTML模板(经过迭代)的输入。

So consumer component will use CardComponent selector and pass both dataArray and Template. 因此,消费者组件将使用CardComponent选择器并传递dataArray和Template。

How can i achieve it? 我该如何实现? (passing htmltemplate) (通过htmltemplate)

You can use ng-content tag. 您可以使用ng-content标签。

Your reusable component (YOUR-REUSABLE-COMPONENT) template is something like this. 您的可重用组件(YOUR-REUSABLE-COMPONENT)模板就是这样。

<div class="box">
    <ng-content></ng-content>
</div>

Usage of your reusable component inside some other component template 在其他组件模板中使用可重用组件

<YOUR-REUSABLE-COMPONENT>
<div class="class1">
<h1>my main component</h2>
</div>
</YOUR-REUSABLE-COMPONENT>

Yes, it is possible to pass HTML template using the ng-content approach but you should always go via template approach. 是的,可以使用ng-content方法传递HTML模板,但是您应该始终使用模板方法。

Your .html file -> Generic -> 您的.html文件->通用->

<div>
      <ng-container *ngTemplateOutlet="template"> </ng-container>
 </div>

Your .ts file 您的.ts文件

    @ContentChild(TemplateRef)
    template: TemplateRef<any>;

and your other component files from where you want to pass the template: 以及要传递模板的其他组件文件

<my-component>
     <ng-template> Your HTML Content </ng-template>
 </my-component>

More details regarding why you should consider ng-template instead of 有关为什么应考虑使用ng-template而不是的更多详细信息

https://medium.com/michalcafe/angulars-content-projection-trap-and-why-you-should-consider-using-template-outlet-instead-cc3c4cad87c9 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 2使用ng-content的“模板”在组件循环内使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular2中将值从一个组件传递到另一个组件? - How to pass value from one component to another component in Angular2? 如何在Angular2中将变量值从一个组件传递到另一个组件 - How to pass variable value from one component to another in Angular2 将参数从一个组件传递到另一个angular2 - pass params from one component to another angular2 如何在angular2中将数组的索引从一个组件传递到另一个组件? - How to pass index of an array from one component to another in angular2? 如何将数据从一个组件传递到另一个 angular2 - How to Pass the data from one component to another angular2 Angular2:组件可以在另一个组件模板中使用吗? - Angular2: can component be used inside another component template? 将字符串从angular2中另一个组件tpl传递给组件 - Pass a string to component from another component tpl in angular2 如何将数据从一个子组件传递到angular2中的另一个子组件? - How to pass data from one child component to another child component in angular2? 如何将变量从一个组件传递到另一组件,并在第二个组件模板html文件中打印该变量? - How to pass variable from one component to another component and print that variable in 2nd Component template html file? 将数值从一个组件发送到Angular2中的另一个组件 - Send a number value from one component to another component in Angular2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM