gpt4 book ai didi

angular - 是否可以在 Angular 2+ 中进行条件内容投影(包含)

转载 作者:太空狗 更新时间:2023-10-29 17:20:20 25 4
gpt4 key购买 nike

我想提供只有在内容未被嵌入时才会出现的默认内容。

例如这是我的组件模板:

<article>
<header>
<ng-content select="[header]"></ng-content>
</header>
<section>
<ng-content></ng-content>
</section>
</article>

我可以这样使用它:

<my-component>
<h1 header>This is my header</h1>
<p>This is my content</p>
</my-component>

现在如果我想提供默认 header 怎么办。是否可以;没有杂技,比如检查 ngAfterContentInit 中的内容?

最佳答案

您可以使用纯 CSS 来做到这一点!假设您有以下内容

<ng-content select=".header"></ng-content>
<h1 class="default-header">
This is my default header
</h1>

如果提供了内容,则以下 CSS 将隐藏 header :

.header + .default-header {
display: none;
}

如果未提供 header ,则不匹配 display:none 规则。

请注意,您必须关闭内容封装才能使用它:encapsulation: ViewEncapsulation.None

关于angular - 是否可以在 Angular 2+ 中进行条件内容投影(包含),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41832199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com