Inject component in angular dialog

Viewed 732

Is it possible to create generic dialog and inject different component into when is necessary.

In example I have dialog and injected component first-component:

<dialog>
 <first-component></first-component>
</dialog>

Or if I want I can reuse dialog and inject another component:

<dialog>
 <second-component></second-component>
</dialog>

Can we dod this in angular material?

1 Answers

There are multiple ways to do it. If you need it in an own UI-library, you probably are looking for <ng-content>, a placeholder-angular-HTML-tag, that can be replaced by any Content(-Child), which is passed from the parent-component down the hierarchy. See as reference: https://angular.io/api/core/ContentChild

Or if you are in a domain component and have a fixed number of possible content, you can use <child-component *ngIf="condition; else #otherComponent">or for more than two possibilities *ngSwitchCase

Alternatively, if you want the content to be controlled by the URL, you can use <router-outlet>; see https://angular.io/guide/router

Related