gpt4 book ai didi

angular - Angular 模板中的可重用片段

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

我们现在可以得到 else片段,指的是 <ng-template> :

<div *ngIf="condition; else not">
Condition satisfied.
<ng-template #not>
Condition not satisfied.
</ng-template>
</div>

我希望能够引用 *ngIf 上下文之外的片段--有点像

<div>DIV1 <ng-template-call template="shared"></ng-template-call></div>
<div>DIV2 <ng-template-call template="shared"></ng-template-call></div>

<ng-template #shared>This is a shared snippet.</ng-template>

写我所谓的ng-template-call的正确方法是什么?以上?

是的,我知道我可以把它变成一个单独的组件,但它不会上升到那个级别。我总是可以写:

<div *ngIf="false; else shared">

但这看起来很笨拙。

最佳答案

我认为 Transclusions 就是您要找的东西

这是一个示例:

<ng-container [ngTemplateOutlet]="reusable"></ng-container>

<ng-template #resusable>...</ng-template>

供引用:https://toddmotto.com/transclusion-in-angular-2-with-ng-content

关于angular - Angular 模板中的可重用片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44917842/

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