I'm using Angular 8
I have a canvas
element which has to be displayed in different child components inside the parent component but the data on it should be same.
To resolve this situation, I used ng-content
in the child components like
Component A and B has HTML contains
<div class="child">
<ng-content select=["canvasPreview]"></ng-content>
</div>
And ParentComponent has
<mat-horizontal-stepper labelPosition="bottom" #stepper (selectionChange)="onSelectionChange($event)">
<mat-step>
<ng-template matStepLabel>Upload Image</ng-template>
<app-upload-background-image
[(previewImage)]="previewImage"
(previewImageChange)="onPreviewImageChange($event)">
</app-upload-background-image>
</mat-step>
<mat-step>
<ng-template matStepLabel>Place Canvas A</ng-template>
<app-a>
<div canvasPreview *ngTemplateOutlet="canvas"></div>
</app-a>
</mat-step>
<mat-step>
<ng-template matStepLabel>Design Canvas B</ng-template>
<app-b>
<div canvasPreview *ngTemplateOutlet="canvas"></div>
</app-b>
</mat-step>
</mat-horizontal-stepper>
<ng-template #canvas>
<app-canvas-child></app-canvas-child>
</ng-template>
The app-canvas-child component has the canvas
element
canvas-child.component.html
<div class="title">Canvas Success</div>
<canvas height="400" width="500">
But in the Parent component, it displays Canvas Success title from the app-canvas-child
component, but the canvas area is blank. Same using in the parent component directly (outside ng-template) works fine and displays the canvas.