3

I am trying to load a component in some other component which has its own view. The component that will be loading will be dynamic and based on some condition.

What I mean is replacing another component at same place inside the parent component.

For instance, In my AppComponent.html (below) I would like to load components which can again be replaced by some other component with some events.

AppComponent.html

<button class="button1">Button1 </button>
<button class="button2">Button2 </button>
<placeHolder></placeHolder>

AppComponent.ts

@Component({
    selector: '...',
    templateUrl: 'resources/app.component.html'
})
export class AppComponent{
     //on click of the button1, I want to load component c1, and on click of button2, I want to load component c2
}

(c1 and c2 are just any other component)

How to do this ? Any help is highly appreciated.

5
  • Please add some code that demonstrates what you try to accomplish. Commented Apr 27, 2016 at 14:52
  • Günter - At first glance, I thought it was a lot different topic (stackoverflow.com/questions/36325212/…), but I guess in your answer where you have have used DynamicComponentLoader, I will try with this and hopefully that will work for me.. though I have made edits as you had asked for more specific query. Hope this makes sense for you. Commented Apr 27, 2016 at 15:17
  • If you want to load and unload at the <placeHolder> tag then DCL (DynamicComponentLoader) should be the right thing for you. Commented Apr 27, 2016 at 15:26
  • Okay, thanks... if you like you can write this as answer since I think this would be a more generic answer, and I think it will be helpful for other users. Commented Apr 27, 2016 at 15:29
  • DCL changed quite a bit in beta.16. I have yet to update the linked answer. stackoverflow.com/questions/35170985/… is already updated in case you need it immediately. Commented Apr 27, 2016 at 15:35

1 Answer 1

1

If you want to load and unload at the <placeHolder> tag, then DCL (DynamicComponentLoader) should be the right thing for you

See Angular 2 dynamic tabs with user-click chosen components for an example.

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.