admin管理员组文章数量:1400202
I'm trying to build a simple tabs-menu in my Angular app.
parantponent.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>
parantponent.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) bee plex and I want to move them to separate ponents.
How could I inject ponent into <ng-container>
??
I'm trying to build a simple tabs-menu in my Angular app.
parant.ponent.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.ponent.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) bee plex and I want to move them to separate ponents.
How could I inject ponent into <ng-container>
??
2 Answers
Reset to default 6To 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.ponent.html:
<p>tab1 works!</p>
tab2.ponent.html:
<p>tab2 works!</p>
parent.ponent.html:
<div>
<button (click)="selectTab(1)">Tab1</button>
<button (click)="selectTab(2)">Tab2</button>
<ng-container *ngComponentOutlet="activeTab">
</ng-container>
</div>
parant.ponent.ts:
import { Tab1Component } from './tab1.ponent';
import { Tab2Component } from './tab2.ponent';
...
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 ponents to the entryComponents
section.
app.module.ts:
import { Tab1Component } from './tab1.ponent';
import { Tab2Component } from './tab2.ponent';
...
@NgModule({
...
entryComponents: [
Tab1Component,
Tab2Component,
...
pnent-templet1 and pnent-templet2 is name of ponent which previously was in ng-templet now you put them in ponent*
<ul>
<li *ngFor='let link of links'>
<ng-container
[ngTemplateOutlet]="link.type == 'plex' ?plexLink : simpleLink"
[ngTemplateOutletContext]="{link:link}">
</ng-container>
</li>
</ul>
<ng-template #simpleLink let-link='link'>
Simple : {{ link.name }}
<pnent-templet2 [input]="link.somevalue"></pnent-templet2>
</ng-template>
<ng-template #plexLink let-link='link'>
Complex : {{ link.name }}
<pnent-templet1 [input]="link.somevalue"></pnent-templet1>
本文标签: javascriptInjecting component to ngcontainer (load ngtemplate from file)Stack Overflow
版权声明:本文标题:javascript - Injecting component to ng-container? (load ng-template from file) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744220257a2595831.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论