没有包装标签的角度转换

时间:2017-12-21 17:57:30

标签: angular angular-template transclusion

如何在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>

这是用数据

填充模板的组件之一
<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>

转换结果看起来如此:

<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>

由于语义和非常敏感的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>

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

1 个答案:

答案 0 :(得分:9)

您可以在ngProjectAs代码

上使用ng-container角度属性
<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>

<强> Stackblitz Example

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