Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valor-software/ngx-bootstrap put a component inside dynamically created content

I'm trying to use valor-software/ngx-bootstrap to create a dynamic tab but I want to put a component's selector inside the dynamically created tab content,

In the documentation example we have:

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
    });
  }

}

And I want to be able to do something like this:

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 sanitizes the component selector to string Is there any workaround?

like image 425
user4092086 Avatar asked Feb 19 '26 09:02

user4092086


1 Answers

Actually I took this approach that doesn't need to path any html in the content

 <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>

So basically I already put all the possible tabs that I may have and based on which one I need to be shown I'll pass the content which act as a switch and in the template there is a switchCase that shows the tab that match the switchCase.

like image 109
user4092086 Avatar answered Feb 22 '26 02:02

user4092086



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!