在我的 Angular 6 应用程序中,我需要将 Component 作为其ng-template传递给另一个 Component 。
原因是我有一个Component A需要多次复制,但每次它都必须包含具有相同 Inputs的不同组件(我们称它们为Component B和Component C)。
组件 A模板:
<div class="row-detail-panel">
<h4 class="row-detail-panel-title">{{ newEntity ? 'Add new' : 'Edit this'}} {{ entityName }}</h4>
<!--THIS IS THE COMPONENT I WANT TO INJECT-->
<app-component-b
[inline]="true"
[form]="form"
></app-component-b>
<!--END-->
<!--some more html code here-->
</div>
我使用以下方法创建了一个组件 A实例:
<app-component-a
[entity]="row"
[entityName]="entityName"
></app-component-a>
于是想到了使用ng-template
,于是把Component A模板改成如下:
<div class="row-detail-panel">
<h4 class="row-detail-panel-title">{{ newEntity ? 'Add new' : 'Edit this'}} {{ entityName }}</h4>
<ng-template></ng-template>
<!--some more html code here-->
</div>
并使用以下方法创建一个组件 A实例:
<app-component-a
[entity]="row"
[entityName]="entityName"
>
<app-component-b
[inline]="true"
[form]="form" <!--PROBLEM: "form" does not exist here-->
></app-component-b>
</app-component-a>
因此,我可以轻松地将Component C而不是Component B作为Component A的 ng-template 注入:
<app-component-a
[entity]="row"
[entityName]="entityName"
>
<app-component-c
[inline]="true"
[form]="form" <!--PROBLEM: "form" does not exist here-->
></app-component-c>
</app-component-a>
问题:
form
我需要注入到组件 B或组件 C的变量仅存在于组件 A内部,而不存在于组件 A 的父组件中(由于某些原因,我无法将其上移一级)。
我怎么解决这个问题?