Typically, if you can't use a component, then you can use a template and containers to display the same content multiple times.
In the example below I replaced the container-2
elements with ng-container
components. Elsewhere I added a ng-template
component and placed container-2
into the content. Finally I associate the two by putting *ngTemplateOutlet
directives on the containers and passing them a reference to the template using a template variable.
<content1>
<mat-horizontal-stepper *ngIf="somevar" >
<ng-container *ngTemplateOutlet="content2Template"></ng-container>
</mat-horizontal-stepper>
<mat-vertical-stepper *ngIf="!somevar" >
<ng-container *ngTemplateOutlet="content2Template"></ng-container>
</mat-vertical-stepper>
</content1>
<ng-template #content2Template>
<content2></content2>
</ng-template>
However, this method won't work if you're using an angular material Stepper and Steps. The reason is because the Step components expect an ancestor Stepper component to be injected into it. Since you want to reuse the templates they have to be outsize of the Steppers so there is no way for you to meet that injection requirements. The method above won't work in any situation where the child component expects the parent to be injected.
The only other solution would be to use templates for the content themselves. So while the step components are repeated the forms inside of them will not. Presumably the forms will be the meat of the content so there won't be too much repetition.
<mat-vertical-stepper> *ngIf="somevar"
<mat-step label="Step 1" >
<ng-container *ngTemplateOutlet="Content1"></ng-container>
</mat-step>
<mat-step label="Step 2">
<ng-container *ngTemplateOutlet="Content2"></ng-container>
</mat-step>
</mat-vertical-stepper>
<mat-horizontal-stepper>
<ng-template #tmplt let-stepper *ngIf="!somevar">
<mat-step label="Step 1" >
<ng-container *ngTemplateOutlet="Content1"></ng-container>
</mat-step>
<mat-step label="Step 2">
<ng-container *ngTemplateOutlet="Content2"></ng-container>
</mat-step>
</ng-template>
<ng-container *ngTemplateOutlet="tmplt"></ng-container>
</mat-horizontal-stepper>
<ng-template #Content1>Content1</ng-template>
<ng-template #Content2>Content2</ng-template>