如何在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的转换。
答案 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 强>