Chris
Chris

Reputation: 1304

Using Two HTML for Single component in Angular 2+?

I am using Angular 4. Is it is possible to use two html file for single component? If yes, can any one tell me how.

Upvotes: 2

Views: 4277

Answers (3)

Frank Thoeny
Frank Thoeny

Reputation: 310

I realize this Angular 2+ post is over a year old, but I hope this helps someone. There is really a Trick to creating components with two or more html files. You'd have multiple @components each followed by classes. This could be used for modal popups or something I guess.

Same file:

import { Component, ... } from '@angular/core';


@Component({
    selector: 'first-component',
    templateUrl: './first.component.html',
    styleUrls: ['./first.component.scss'],
})
export classFirstComponent {

   constructor() {}
   ... Doing something here.

}

@Component({
    selector: 'second-page',
    templateUrl: './second-page.html',
})

export class SecondPage {

   constructor() {}
   ... Doing something here.
}

Upvotes: 0

imdisney
imdisney

Reputation: 109

Sorry, but you cannot use two *.html files for a single component.

If there is concept of master page & content page then I can suggest you something

Upvotes: 1

Pubudu Dodangoda
Pubudu Dodangoda

Reputation: 2874

Even if you find a tricky way to do so, it will be an anti pattern, meaning that you will face bigger trouble as the application grows.

I recommend you to create two components and localize the common logic in a service. Read more about services at https://www.tutorialspoint.com/angular2/angular2_services.htm

Or you can create a new component and use a child view as pointed out by @Muthu

Upvotes: 1

Related Questions