Valor-software / ngx-bootstrap将一个组件放入动态创建的内容中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Valor-software / ngx-bootstrap将一个组件放入动态创建的内容中相关的知识,希望对你有一定的参考价值。
我正在尝试使用valor-software/ngx-bootstrap创建动态选项卡,但我想将组件的选择器放在动态创建的选项卡内容中,
在文档示例中,我们有:
import { Component, ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'demo-tabs-dynamic',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './dynamic.html'
})
export class DemoTabsDynamicComponent {
tabs: any[] = [
{ title: 'Dynamic Title 1', content: 'Dynamic content 1' },
{ title: 'Dynamic Title 2', content: 'Dynamic content 2', disabled:
true },
{ title: 'Dynamic Title 3', content: 'Dynamic content 3',
removable: true }
];
addNewTab(): void {
const newTabIndex = this.tabs.length + 1;
this.tabs.push({
title: `Dynamic Title ${newTabIndex}`,
content: `Dynamic content ${newTabIndex}`,
disabled: false,
removable: true
});
}
}
我希望能够做到这样的事情:
import { Component, ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'demo-tabs-dynamic',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './dynamic.html'
})
export class DemoTabsDynamicComponent {
tabs: any[] = [
{ title: 'Dynamic Title 1', content: 'Dynamic content 1' },
{ title: 'Dynamic Title 2', content: 'Dynamic content 2', disabled:
true },
{ title: 'Dynamic Title 3', content: 'Dynamic content 3',
removable: true }
];
addNewTab(): void {
const newTabIndex = this.tabs.length + 1;
this.tabs.push({
title: `Dynamic Title ${newTabIndex}`,
content: `<my-component></my-component>`, // Here is the change
disabled: false,
removable: true
});
}
}
Angular将组件选择器清理为字符串是否有任何解决方法?
答案
实际上我采用这种方法不需要在内容中路径任何html
<tabset >
<tab *ngFor="let tabz of mainMenuTab.tabs"
[heading]="tabz.title"
[active]="tabz.active"
(select)="tabz.active = true"
(deselect)="tabz.active = false"
[disabled]="tabz.disabled"
[removable]="tabz.removable"
(removed)="removeTabHandler(tabz)"
[customClass]="tabz.customClass">
<div [ngSwitch]="tabz?.content">
<app-employees-menu *ngSwitchCase="'employee'"></app-employees-
menu>
<app-inventories-menu *ngSwitchCase="'inventory'"></app-
inventories-menu>
<app-customers-menu *ngSwitchCase="'customer'"></app-customers-
menu>
</div>
</tab>
</tabset>
所以基本上我已经把所有可能的选项卡放在我需要显示的位置上,我将传递作为交换机的内容,在模板中有一个switchCase,显示与switchCase匹配的选项卡。
以上是关于Valor-software / ngx-bootstrap将一个组件放入动态创建的内容中的主要内容,如果未能解决你的问题,请参考以下文章