0

I receive all components to show at a particular time so I'm iterating it. I have tried it like shown below.I already have Component created just which Component are allowed to use is from server

      <ngb-tabset [activeId]="1">
        <ngb-tab id="{{i+1}}" title="{{dir.Name}}" *ngFor="let dir of DefaultDirectives;let i=index">
          <ng-template ngbTabContent>
            //Custom Component According to name of directive name in iteration
          </ng-template>
        </ngb-tab>
      </ngb-tabset>

I have for example this :

 DefaultDirectives:any[]=[
         {Name:'First Directive',Directive:'app-dir1'},
         {Name:'Third Directive',Directive:'app-dir3'},
       ]

I have all these components

@Component({
  selector: 'app-dir1',
  templateUrl: './dir1.component.html', 
})
export class Dir1Component  {
//Some Code
}



@Component({
  selector: 'app-dir2',
  templateUrl: './dir2.component.html', 
})
export class Dir2Component  {
//Some Code
}



@Component({
  selector: 'app-dir3',
  templateUrl: './dir3.component.html', 
})
export class Dir3Component {
    //Some Code
}
6
  • You should learn the difference between a component and a directive. angular.io/api/core/Directive Commented Dec 27, 2019 at 11:42
  • What can I do to make it like this to call as component I can do <app-dir1></app-dir1> but I Selector is dynamic @Reactgular Commented Dec 27, 2019 at 11:44
  • 1
    There are a lot of different ways to do what you want here. Ranging from a switch to dynamic components. It looks like you want dynamic, but a ngSwitch is easiest (why complicate things?). Anyway, read this: angular.io/guide/dynamic-component-loader Commented Dec 27, 2019 at 11:45
  • because there are 20-30 different Template which I have to use usually 3-5 will be there Commented Dec 27, 2019 at 11:46
  • If I use switch case the code will increase Commented Dec 27, 2019 at 11:47

1 Answer 1

2

Based on what is explained in https://angular.io/guide/dynamic-component-loader, I once wrote a DynamicHost directive helper (in Angular 6 project)

@Directive({
    selector: '[smaDynamicHost]',
})
export class DynamicHostDirective implements OnInit {
    @Input('smaDynamicHost') component: Type<any>;

    constructor(
        private componentFactoryResolver: ComponentFactoryResolver,
        private viewContainerRef: ViewContainerRef,
        private cdr: ChangeDetectorRef,
    ) {}

    ngOnInit(): void {
        const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
            this.component,
        );
        this.viewContainerRef.clear();
        this.viewContainerRef.createComponent(componentFactory);
        this.cdr.detectChanges();
    }
}

With that directive, you can now simply pass the type of component you want to load dynamically. With your example, it should looks like

<ng-template ngbTabContent [smaDynamicHost]="dir.Directive"></template>
 DefaultDirectives:any[]=[
    {Name: 'First Directive', Directive: Dir1Component},
    {Name: 'Third Directive', Directive: Dir3Component},
]

Important: you must declare your dynamic components as entryComponents in your angular module

I hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.