Injecting component to ng-container? (load ng-template from file)
Asked Answered
B

2

2

I'm trying to build a simple tabs-menu in my Angular app.

parant.component.html:

<div>
  <button (click)="selectTab(1)">Tab1</button>
  <button (click)="selectTab(2)">Tab2</button>

  <ng-container *ngTemplateOutlet="(selected == 1) ? template1 : template2">
  </ng-container>

  <ng-template #template1>I'm page 1</ng-template>
  <ng-template #template2>I'm page 2</ng-template>
</div>

parant.component.ts:

public selected = 1;
public selectTab(tabName) {
  this.selected = tabName;
}

This is working fine, as long as the <ng-template> is part on the same page html. Now my pages (the content of #template1 and #template2) become complex and I want to move them to separate components.

How could I inject component into <ng-container>??

Business answered 4/12, 2019 at 12:24 Comment(0)
B
6

To inject a Component into <ng-container>, you didn't need to use *ngTemplateOutlet, instead use *ngComponentOutlet.

You can see the full API at: NgComponentOutlet

tab1.component.html:

<p>tab1 works!</p>

tab2.component.html:

<p>tab2 works!</p>

parent.component.html:

<div>
  <button (click)="selectTab(1)">Tab1</button>
  <button (click)="selectTab(2)">Tab2</button>

  <ng-container *ngComponentOutlet="activeTab">
  </ng-container>
</div>

parant.component.ts:

import { Tab1Component } from './tab1.component';
import { Tab2Component } from './tab2.component';
...

public activeTab = Tab1Component;

public selectTab(tabName) {
  if (tabName == 1) {
    this.activeTab = Tab1Component ;
  }
  else if (tabName == 2) {
    this.activeTab = Tab2Component ;
  }  
}

And don't forget to add these dynamic components to the entryComponents section.

app.module.ts:

import { Tab1Component } from './tab1.component';
import { Tab2Component } from './tab2.component';
...
@NgModule({
  ...
  entryComponents: [
    Tab1Component,
    Tab2Component,
    ...
Business answered 4/12, 2019 at 16:31 Comment(0)
P
1

compnent-templet1 and compnent-templet2 is name of component which previously was in ng-templet now you put them in component*

 <ul>
            <li *ngFor='let link of links'>
                <ng-container 
                     [ngTemplateOutlet]="link.type == 'complex' ?complexLink : simpleLink" 
                     [ngTemplateOutletContext]="{link:link}">
                </ng-container>
            </li>
        </ul>

        <ng-template #simpleLink let-link='link'>
            Simple : {{ link.name }}
    <compnent-templet2 [input]="link.somevalue"></compnent-templet2>
        </ng-template>

        <ng-template #complexLink let-link='link'>
            Complex : {{ link.name }}
    <compnent-templet1 [input]="link.somevalue"></compnent-templet1>

Prophetic answered 4/12, 2019 at 12:54 Comment(2)
But if I have 15 tabs, I need to add them all the the same html? can't I separate them in separate files?Business
you can create one component which will be responsible to inject right component , you can pass some in put value to the component then that componet will decide which component actually need to part of ng-templatProphetic

© 2022 - 2024 — McMap. All rights reserved.