Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Dynamically Instantiate an Angular Component

    5 - 6

    In this lesson we learn how to leverage the ComponentFactoryResolver as well as our anchor we defined previously, to dynamically instantiate an Angular component, in our specific case the TabComponent.

    We will also learn how to register our dynamic components in the NgModule’s entryComponent property.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: In our example here, we have most of the building blocks, which help us to build actual dynamic components. First of all, here we have a way of defining our template. We then reference the template and pass it along, in this case via some input property to our tab.

    That's also the tab component itself. The single tab here is ready for being created dynamically because we have already defined here some input properties, which allow us to pass in the template, add data. Then, inside there in the template of our tab component, we actually can use the ng-container.

    Add template outlet for referencing the template. Add template outlet context for passing in the actual data, which we get from the outside.

    Also, in our tabs component -- the whole component which groups all of the single tabs in itself -- is ready. What we have done there is, we have an ng-template, where we have attached some dynamic tab anchor.

    That's an anchor directive, which we reference here, and which allows us then to later get access to the so-called ViewContainerRef, which we will need for then attaching our dynamic components onto it.

    Let's get started with actually instantiating our component dynamically. First of all, we need to get access to the component factory resolver. We get that injected here in the constructor. Then, further down, we can remove that log file here and start actually using that component factory.

    Here, we basically create a new factory out of our component, which in this case is basically our tab component, because that's the one which you want to create dynamically.

    Once we have that, we can get access to that ViewContainerRef by using our anchor because we will then need that ViewContainerRef to actually create the so-called ComponentRef. We use that ViewContainerRef.createcomponent and we pass in our component factory, of course.

    At this point, we basically have here a component ref instance. Now, to get the actual instance of our tab component, we can do something like componentref.instance.

    Now, here we have the living instance of our tab component in memory. As you have seen, the tab component has various properties. If we do instance-dot, we see here we have to associate the title, the template, and data context. These are the things which we obviously want to pass from the outside.

    Let's add here some parameters. Here's the string. We have the template and some data. Now, we can associate them and say, "Tell title equals to title, instance.template is equal to template." Great.

    Now, we have wired them up. Let's then create some local area for keeping track of our dynamic components. This is useful for later then interacting with a single dynamic component.

    What we also would like to have is that whenever our tab gets instantiated here and added to our view, it should also get the focus. We can directly use that select tab function. Let's simply pass in the dynamic tab here, which you have just created. Cool.

    We also need to do some housekeeping here. What you see here is that in the select tab, we have basically our tabs array, which here in our static part gets directly injected here by the content projection. We grab it via the content controller.

    We have a second array here, which is the dynamic tabs. What we have to do, first of all, is we have to generate here the header sections also for our dynamic tabs. I simply copy and paste this in here. Here, we use the dynamic tabs. Obviously, you could merge those two, so you can avoid some code duplication here. For the purpose of this example, let's just clone it here.

    Similarly, we also need to do here the same thing when we select a tab. We don't just have to iterate over the tabs, but also the dynamic tabs, and to deactivate them accordingly. Great.

    Now, we should actually be able to use it and invoke that open tab function from our app component. Let's jump there and start implementing it here.

    As you can see, the first one here is the title. Let's simply input here dynamic tab. Then we can use the template. If you remember, template has already been defined here by using this view chart and here by referencing this ng-template in our template section of this app component. Let's simply use that one.

    As a last step, there is the data, which we have to pass in. Here, we can simply use our people array and access the first person.

    Now, once the browser refreshes, if we click now, "Add new person," you can see that the tab opens dynamically. You can also see here, "Hi, I am Yuri."