2

It seems to be basic problem, but i couldnt find simple solution.

I have one root component (something like faceted search container) and two different sub components which i want to use conditionally (depends on parameter in root component).

Simple components diagram

These sub components are different types of search results for common root component. How to handle this situation ? Some kind of multiple templates for one component or conditionally rendered directives of subcomponents ?

I appreciate every answer. This is my first Angular2 project so sorry if this is too trivial for someone :) .

Greetings

Sarvesh Yadav
  • 2,600
  • 7
  • 23
  • 40
bero
  • 57
  • 2
  • 6

1 Answers1

2

If you know what component types should be added at build time, then add them to the components template and use *ngIf or *ngSwitchCase to show them depending on a condition.

If you want to be able to pass different components from the outside to be displayed in the inside you can use

<ng-content>

or adding components dynamically like shown in Angular 2 dynamic tabs with user-click chosen components

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567