Valor-software / ngx-bootstrap将一个组件放入动态创建的内容中

问题描述 投票:3回答:1

我正在尝试使用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将组件选择器清理为字符串是否有任何解决方法?

angular tabs angular-components ngx-bootstrap
1个回答
5
投票

实际上我采用这种方法不需要在内容中路径任何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匹配的选项卡。

© www.soinside.com 2019 - 2024. All rights reserved.