3

Is there a way for Angular 2 component to use many template files base on where i want to place it?

For example, I have a login component and i want to place it two times on my website with two different designs.

Is there a way that i can pass template to a component?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Maxvt
  • 311
  • 4
  • 11

2 Answers2

6

Not sure if NG2 has built-in way to support this. I just used a base component class to contain all or most logic and data, without a html template. Then in derived component classes, just need to declare constructor calling super(...), and define respective html template.

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

ZZZ
  • 2,752
  • 2
  • 25
  • 37
-7

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>
Alexis Le Gal
  • 327
  • 4
  • 11
  • Since no one can give me better answer. I am making this one as accepted. Thank You for help. – Maxvt Sep 05 '16 at 12:26
  • this does not answer the question. This is using parts of the same template to show conditional information. – Pac0 Nov 03 '17 at 10:38