Can a single component have multiple patterns?

Is there a way for the Angular 2 component to use many template files based on where I want to place it?

For example, I have a component loginin loginand I want to place it twice on my site with two different designs.

Is there a way I can pass a template for a component?

+3
source share
2 answers

Not sure if NG2 has a built-in way to support this. I just used a base component class containing all or most of the logic and data without an html template. Then, in the derived component classes, you just need to declare a constructor that calls super (...) and define the appropriate html template.

If your application is complex, you will probably use a module to instantiate classes, then make sure you declare moduleId: module.id in the Component attribute, otherwise NG2 runtime might complain that the html template could not be loaded .

+5
source

The simplest way to do this, I think, is using * ngIf in your template.

<div *ngIf="template === 1"> First Template </div>
<div *ngIf="template === 2"> Second Template </div>
-6
source

Source: https://habr.com/ru/post/1653133/


All Articles