1

I am trying to get a component dynamically from a string depending on the client.

import { C22Component } from '../client/c22/c22.component';
import { C26Component } from '../client/c26/c26.component';

@Component({
  selector: 'special-client',
  template: '<div #specialView></div>'
})
export class SpecialClientComponent {
  @Input() clientId:number;

  getComponent() {
    return `C${this.clientId}Component`;
  }
}

I tried to use the eval() function but it did not work.

Thank you.

  • How do you want to use `getComponent`? – Günter Zöchbauer Jan 16 '17 at 09:37
  • 1
    You can maintain a map from string to component type and than you an approach as demonstrated inhttp://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468 – Günter Zöchbauer Jan 16 '17 at 09:37
  • You could also use a template containing `*ngIf` or `*ngSwitch` which tests for the `clientId` and then simply reference/include the appropriate component inside the `*ngIf` / `*ngSwitch` branch. – BlueM Jan 16 '17 at 10:09

0 Answers0