Mayur Ingole
Mayur Ingole

Reputation: 1

Is there any way to render two or more templates from one component? Angular2

As I am creating components through cmd (ng g component test), it will create .css .html and .ts files for that particular test component.

I want to render two different templates based on some conditions.

As by default it will render own html template, along with that I want to render different template. So can I add one more html file in same component and render that??

So, is there any way to render two or more templates from one component?

Upvotes: 0

Views: 179

Answers (1)

Juan Garcia
Juan Garcia

Reputation: 59

You can create two component with different template and call these other component from your father component like this:

@Component({
    selector: 'data-list',
    template: `
        <childComponent1></childComponent1>
        <childComponent2></childComponent2>
     `
})

ChildComponent1:

@Component({
selector: 'childComponent1',
template: `
    <h1>Hiiiiiiii</h1>
`
})

ChildComponent2:

   @Component({
    selector: 'childComponent2',
    template: `
    <h2>I'm second component</h2>
          `
    })

This is the unique way to use two or more templates in same component with Angular.

Upvotes: 2

Related Questions