【问题标题】:Accessing child Components inside the parent component's html element访问父组件的 html 元素内的子组件
【发布时间】:2020-03-29 01:58:14
【问题描述】:

我的要求是生成一个组件,该组件可以访问和呈现在标记中添加到它的子组件。

HTML

<my-parent>
    <span>hey</span>
    <span>heck</span>
</my-parent>

呈现的 HTML

<div>
    <span>before</span>
    <span>hey</span>
    <span>after</span>
    <br />
    <span>before</span>
    <span>heck</span>
    <span>after</span>
</div>

我找不到任何资源来帮助我。这种行为类似于 mat select 与 mat 选项的交互方式。

【问题讨论】:

  • Angular 源代码是免费提供的——你看过 mat select 的源代码吗?

标签: html angular angular-material


【解决方案1】:

使用ng-contentselect 作为demoed here

<my-parent>
  <span a>hey</span>
    <span b>heck</span>
</my-parent>
  <div>
    <span>before - </span>
    <ng-content select="[b]"></ng-content>
    <span> - after</span>
    <br />
    <span>before - </span>
    <ng-content select="[a]"></ng-content>
    <span> - after</span>
</div>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
  • 2019-06-30
  • 1970-01-01
相关资源
最近更新 更多