没有包装标签的角度转换

Gen*_*yuk 4 transclusion angular-template angular

如何在Angular中进行插槽转换而不包括包装标签?

例如:

这是带选择器的组件模板my-component:

<div class="my-component">
   <p class="some sensitive css-classes">
       <ng-content select="sub-header"></ng-content>
   </p>

   <p class="more sensitive css-classes">
       <ng-content select="sub-footer"></ng-content>
   </p>
</div>
Run Code Online (Sandbox Code Playgroud)

这是用数据填充模板的组件之一

<my-component>
    <sub-header>
        Very <strong>important</strong> text with tags.
    </sub-header>

    <sub-footer>
        More <em>important</em> text with tags.
    </sub-footer>
</my-component>
Run Code Online (Sandbox Code Playgroud)

转换结果如下:

<div class="my-component">
   <p class="some sensitive css-classes">
       <sub-header>
          Very <strong>important</strong> text with tags.
       </sub-header>
   </p>

   <p class="more sensitive css-classes">
       <sub-footer>
           More <em>important</em> text with tags.
       </sub-footer>
   </p>
</div>
Run Code Online (Sandbox Code Playgroud)

由于语义和非常敏感的CSS样式,这不是很有用

如何进行如下所示的转换:

<div class="my-component">
   <p class="some sensitive css-classes">
       Very <strong>important</strong> text with tags.
   </p>

   <p class="more sensitive css-classes">
       More <em>important</em> text with tags.
   </p>
</div>
Run Code Online (Sandbox Code Playgroud)

与其他问题的主要区别在于dom的转换.

yur*_*zui 9

您可以ngProjectAsng-container标记上使用angular属性

<my-component>
  <ng-container ngProjectAs="sub-header">
    Very 
    <strong>important</strong> text with tags.
  </ng-container>
  <ng-container ngProjectAs="sub-footer">
    More 
    <em>important</em> text with tags.
  </ng-container>
</my-component>
Run Code Online (Sandbox Code Playgroud)

Stackblitz示例

有关文档,请查看https://github.com/angular/angular.io/issues/1683

  • Kinda感到失望的是,没有关于此的文档,因为没有为此指定的位置(因为v2已经存在)https://github.com/angular/angular.io/pull/2851。对于任何需要更多信息的人,请查看https://medium.com/claritydesignsystem/ng-content-the-hidden-docs-96a29d70d11b (2认同)