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

时间:2017-12-21 04:31:04

标签: angular tabs angular-components 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将组件选择器清理为字符串是否有解决方法?

1 个答案:

答案 0 :(得分: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匹配的选项卡。