【问题标题】:How parent component pass HTML to child component in Angular?Angular 中父组件如何将 HTML 传递给子组件?
【发布时间】:2018-11-28 22:48:17
【问题描述】:

父组件如何将多个ng-templates传递给子组件?

示例 Parent 组件包含多个 ng-templates:

<app-childcomponent>
    <ng-template>Item A</ng-template>
    <ng-template>Item B</ng-template>
    ...
</app-childcomponent>

子组件需要从父组件接收ng-templates并放入app-menu

 <app-menu>
    <ng-content></ng-content>
 </app-menu>

所以它在子组件中看起来像这样:

<app-menu>    
   <ng-template>Item 1</ng-template>     //having trouble sending this to app-menu from parent
   <ng-template>Item 2</ng-template>     //having trouble sending this to app-menu from parent
<app-menu>

但似乎ng-content 是空的,导致app-menu 没有得到multiple ng-templates

 <app-menu>
    //empty
 </app-menu>

我尝试了什么?

  1. 我尝试通过@input 传递模板。 &lt;app-childcomponent [myTemplate]="myTemplate"&gt;&lt;/child-component&gt;

  2. 模板出口。结果:子组件无法获取ng-templates

父 html:

<app-childcomponent>
    <ng-template>Item 1</ng-template>
    <ng-template>Item 2</ng-template>
</app-childcomponent>

父类:

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

子 html:

<app-menu>
<ng-template [ngTemplateOutlet]="templateRef"></ng-template>
<app-menu>

预计&lt;ng-template [ngTemplateOutlet]="templateRef"&gt;&lt;/ng-template&gt; 包含

<ng-template>Item 1</ng-template>
<ng-template>Item 2</ng-template>

但它是空的。

回复@Ricardo 解决方案

我试过你的update。它也是空的。

家长:

<app-childcomponent top-left-menu>
    <ng-template>Item 1</ng-template>
    <ng-template>Item 2</ng-template>
</app-childcomponent>

孩子:

<ng-content select="[top-left-menu]"></ng-content>

我还尝试将ng-template 传递给ng-content,但Print ME! 没有被渲染。它是空的。好像ng-template 不进入ng-content

家长:

<app-childcomponent [contextMenuSubject]="contextmenuSubject">
    <ng-template>Print ME!</ng-template>
</app-childcomponent>

孩子:

<ng-content></ng-content>

【问题讨论】:

  • 如果您将其作为输入参数发送,那么您应该使用 NgTemplateOutlet 从模板动态创建组件。你的结果是什么?
  • 我将它作为input 参数发送并作为 Input()myTemplate 类型的 ElementRef 接收并尝试使用 {{ myTemplate }} 绘制它。我想这是错误的 X.X
  • blog.angular-university.io/… ng-template 和 ngTemplateOutlet 简单解释

标签: javascript html angular typescript


【解决方案1】:

您可以在您的子组件中声明一个具有属性 ngTemplateOutlet 的默认模板,以便您可以从父组件绑定自定义模板

<ng-template [ngTemplateOutlet]="template || defaultTemplate"></ng-template>

<ng-template #defaultTemplate let-item="item">
    {{item}}
</ng-template>

您应该像变量一样公开 模板,以便可以使用父组件来注入自定义模板。

父组件应该是这样的

<child-component [template]="customTemplate" ></child-component>
 <ng-template #customTemplate let-item="item">
      {{item.name}}
 </ng-template>

其他解决方案

在您的子组件中,您可以像这样声明 ng-content:

 <ng-content select="[top-left-menu]"></ng-content>

那么在你的父组件中,你可以这样使用他的引用:

<custom-super-component top-left-menu></custom-super-component>

您的自定义 html/组件将放置在您的 ng-content 的位置

【讨论】:

  • 嗨,听起来不错!只是好奇是否有任何方法可以将template 放在child-component 标签内? 或任何更清洁的方式来做到这一点?
  • @Zainu 你可以使用 标签来做这件事,用一个名字声明这个标签,然后从父组件中引用这个 ng-content 标签到你想要放置的 html 模板中里面
  • 嗨,我已经尝试过 chilldcomponent &lt;ng-content [ngTemplateOutlet]="customTemplate"&gt;&lt;/ng-content&gt; 和`@ContentChild(TemplateRef) customTemplate: TemplateRef`。在父组件&lt;child-component&gt; &lt;ng-template&gt;Item 1&lt;/ng-template&gt; &lt;ng-template&gt;Item 2&lt;/ng-template&gt; &lt;/child-component&gt;
  • 我已经更新了上面的Replying to @Ricardo solution ^
  • 我发现我无法使用ng-container 包装多个ng-template 以作为input(): TemplateRef 发送给孩子。
猜你喜欢
  • 2016-06-10
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 2020-03-30
  • 1970-01-01
  • 2022-11-24
相关资源
最近更新 更多