Angular ng模板中的ng内容始终创建内容,即使未呈现模板

Angular ng模板中的ng内容始终创建内容,即使未呈现模板,angular,angular2-template,ng-template,angular2-ngcontent,Angular,Angular2 Template,Ng Template,Angular2 Ngcontent,我面临一个奇怪的bug,它的ng内容包装在ng模板中 假设我有一个组件内部组件,它显示在外部组件中: <ng-container *ngIf="condition" [ngTemplateOutlet]="test"></ng-container> <ng-template #test> <inner-component></inner-component> </ng-templat

我面临一个奇怪的bug,它的
ng内容
包装在
ng模板

假设我有一个组件
内部组件
,它显示在
外部组件
中:

<ng-container *ngIf="condition" [ngTemplateOutlet]="test"></ng-container>
<ng-template #test>
    <inner-component></inner-component>
</ng-template>
并且,仍然使用
条件=false
,我写道:

<outer-component>
    <inner-component></inner-component>
</outer-component>

然后,令我惊讶的是,创建了
内部组件
,即使它从未渲染过。这对我来说是个问题,因为内部组件(我无法修改的第三方组件)确实需要在应用程序中呈现时创建


你能想出一个我可以在外部组件上使用的解决方案,以避免创建ng内容(显然仍然使用转换-第一个解决方案不是一个选项)。

你可以将模板移动到父组件

<app-outer>
    <ng-template #test>
        <app-inner></app-inner>
    </ng-template>
</app-outer>

Hmm,在
测试
模板中,没有我的内部组件在正确的位置。如果条件为
true
Facepalm,则应在第一个ng容器中呈现此
test
模板。更新答案,我想这就是为什么。你的答案是正确的,但是我希望有一种方法来保持当前的API为我的外部组件(即避免NG模板包装内部组件)@ ErICeLeBeNuthe夫妇的事情,你也可以考虑是动态组件加载,或自定义结构指令。我以前也遇到过同样的问题。也许你在.ts condition=“false”(字符串)中写过——通常,当从服务接收到数据并且是字符串时会发生这种情况——或者你有一个名为condition的模板引用变量——哦,我明白你现在在做什么了!您不希望
内部
组件在外部的外部进行初始化,而是在
外部组件
的模板中使用一个围绕
ng content
的条件进行初始化。是的,经过多次尝试后,我认为这实际上与这里描述的问题相同:除了我无法使用建议的解决方案之外。。。
<app-outer>
    <ng-template #test>
        <app-inner></app-inner>
    </ng-template>
</app-outer>