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.


    Select From Multiple Nested Angular 2 Elements

    2 - 6

    You have complete control over the elements you nest inside of your component instance by using selectors to access and rearrange them. Selecting allows you reconstruct the elements in whatever order you like so you can visual customize the content passed in.



    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




    You could actually pass more stuff into widget two, isn't just one thing. If I pass in an H2 and say title, I'll pass in a div, and say some content, and then an H3 that just says footer. If I go into my widget two template now, you'll see that everything works the same.

    It takes all three pieces, title, some content, footer, and that there's still a between above and below. I can actually select each of those with an NG content tag. I can say select H2, then if I hit save here, and when this refreshes, you'll see this just takes the title, and ignores the other pieces.

    What that allows me to do is if I duplicate this, and I say div, I can grab the div as well. Now I have title and some content. Obviously, if I do that again, and grab the H3, then I'll get each of those titles, some content, and footer.

    I can move these around now. I'll cut this and paste it beneath that H3. This'll move the title to the bottom, underneath the footer. I can grab each piece of content, and reorganize it, even though back in my home component, you can see these are still in the same place.

    As you pass stuff in, you can reorganize and reorder the things inside of there. Also, you don't have to do this by H2, div, H3. You can select any sort of attribute on there. Right now, forcing someone to pass in an H2, H3, and div is a weird contract between templates.

    Instead, if I say this will have an attribute of header, this will have an attribute of content, and this will have an attribute of footer, then inside of here, I could say select the attribute of header, select the attribute of content, and select the attribute of footer.

    I'll hit save, and you can see now I get back to my title, some content, and footer. I could even reorganize them here. If I put content at the top, title at the bottom, hit save, you'll see you'll still get the same order inside of the template, because it's grabbing each of those, and reordering them as expected, header, content, then footer.

    These can be in any order you need. Put that back here in the middle, and clean this up a bit. Lastly, as you might expect, you could also do a class of header, and then in the widget, instead of the square brackets around it as the selector, put the dot as a class selector.

    I'll hit save now, and we still get that same behavior, because it's selecting based on this selector, finding the header, which is the class header, and the other ones are doing the attribute selectors. Those selectors will work pretty much as you need.

    I'm just going to revert it back to the attribute, and back to the attribute.