0

I have a shared Component that can show a toggle button, this toggle button let you switch between two childs components, this two components must be passed as input from the parents that call the SharedComponent. Please any idea ? enter image description here

1 Answers1

0

I think ngComponentOutlet ist what you are looking for: https://angular.io/api/common/NgComponentOutlet

You can then do something like this:

<a (click)="doSwitch()">Switch</a>
<ng-container *ngComponentOutlet="component1"></ng-container>
<ng-container *ngComponentOutlet="component2"></ng-container>

in typescript:

Input()
component1: Type<any>;

Input()
component2: Type<any>;

...

when using the component:

<switch-component [component1]="comp1" [component2]="comp2"></switch-component>

comp1: Type = ParentComponent1;
comp2: Type = ParentComponent2;
Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54
  • Thank you Franziskus, but how to passe component and attributes as parameters. – ABDERRAHIME FARHANE Sep 20 '17 at 15:21
  • I added some code on how to use the component. I dont know if it is possible to bind parameters to the components inside NgComponent. Maybe you shouldn't use inputs in those components. Maybe its better to go with Services or Events here. – Franziskus Karsunke Sep 21 '17 at 11:28