【问题标题】:Valor-software/ngx-bootstrap put a component inside dynamically created contentValor-software/ngx-bootstrap 将组件放入动态创建的内容中
【发布时间】:2018-06-03 17:14:10
【问题描述】:

我正在尝试使用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 mat-tab


    【解决方案1】:

    其实我采用了这种不需要在内容中路径任何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。

    【讨论】:

    • 这是一个很好的解决方案,但我怀疑无论您设置什么条件,所有组件都可能会启动。纠正我@user4092086
    • @AbhijitMuke 不,它们不会被启动,当条件为真时,它们只会启动一次,所以这就像我已经测试过的延迟加载
    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2014-05-24
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 2021-06-23
    相关资源
    最近更新 更多