2

I would like to dynamically load a component. So I tried this approach but it didn't worked:

<page-{{current_page}}></page-{{current_page}}>

Currently I'm using this approach but it doesn't seems efficient:

<div *ngIf="current_page==1">
    <page-1></page-1>
</div>
<div *ngIf="current_page==2">
    <page-2></page-2>
</div>
...
1
  • Why you need dynamic directives? Commented Nov 18, 2016 at 11:33

1 Answer 1

3

This is not supported. String interpolation {{}} can only be used for property-values and element-content, but not for element-names or attribute-names.

Perhaps ViewContainerRef.createComponent() can do what you need.

For an example see Angular 2 dynamic tabs with user-click chosen components

If you have a limited set of components you want to display, *ngIf or ngSwitchCase is the way to go.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.