In my Angular 6 application, I am faced with the challenge of passing a Component to another Component as an ng-template. The scenario involves having a Component A that needs to be replicated multiple times, each time with different components (referred to as Component B and Component C) that share the same Inputs.
The template structure for Component A is as follows:
<div class="row-detail-panel">
<h4 class="row-detail-panel-title">{{ newEntity ? 'Add new' : 'Edit this'}} {{ entityName }}</h4>
<!--THE COMPONENT TO BE INJECTED-->
<app-component-b
[inline]="true"
[form]="form"
></app-component-b>
<!--END-->
<!--additional HTML code here-->
</div>
To create instances of Component A, the following syntax is used:
<app-component-a
[entity]="row"
[entityName]="entityName"
></app-component-a>
To address this requirement, I decided to utilize ng-template by modifying the Component A template as shown below:
<div class="row-detail-panel">
<h4 class="row-detail-panel-title">{{ newEntity ? 'Add new' : 'Edit this'}} {{ entityName }}</h4>
<ng-template></ng-template>
<!--additional HTML code here-->
</div>
Instances of Component A can now be created using the following approach:
<app-component-a
[entity]="row"
[entityName]="entityName"
>
<app-component-b
[inline]="true"
[form]="form" <!--ISSUE: "form" is not accessible here-->
></app-component-b>
</app-component-a>
This setup allows for easy injection of Component C instead of Component B within Component A's ng-template:
<app-component-a
[entity]="row"
[entityName]="entityName"
>
<app-component-c
[inline]="true"
[form]="form" <!--ISSUE: "form" is not accessible here-->
></app-component-c>
</app-component-a>
Challenge:
The variable "form" needed for injection into Component B or Component C only exists within Component A and not in its parent (due to specific reasons preventing it from being moved up a level). How can this problem be resolved?