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.


    Assign a Structual Directive a Dynamic Context in Angular


    Just like passing in an array to *ngFor, you can pass in any value into your structural directive so that it can render templates based on those values. It's crucial to understand how the *directive syntax expands into a <template> and adds a custom @Input based on the syntax you use so that you can use your own data.



    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




    Now, you probably don't want to hard code these values into your directive. You'd probably rather pass them in just like Angu 4 does. If I say let in messages from messages here, what this is going to expand to now is on the template.

    There's going to be an input called three from a combination of the name of the directive, and the separator here which is going to take this as the input. Remember, this was used to declare a variable, and any word you put here is going to be an input which combines this and this. It'd be three from messages like so.

    What that means is we need to change our input from three to three from. Then this value can come from messages which we'll create down here. We'll say messages one, one is awesome, two, two is better, three, three is best.

    Now, this object is going to be past into my three from. In here, I'm going to destructure this where I have a one, two and three, and take the one, and I'll put it here, a two here, three here. Now when I save, we'll have one is awesome, two is better, and three is best.

    This object coming in is this messages coming in here. This past into here, past into three from which is an input being assigned to this, and rendered here through this destructuring, and then assignment to the implicit.

    The one thing you do need to look out for is if the data changes. Say for example in the constructor, you have a set interval, and we'll say every one second, change these messages to...Scrub this, and change some of it.

    I'll just save bad, worse, worst. Then when I hit save now, you'll see that every second, it's just creating more of them. That's not what I really wanted. I wanted it to replace. The way you replace is just by saying this view.clear. Hit save.

    Now, each time that data changes, it will clear out, and just replace what's currently there essentially recreating all these views from the templates. Just to really prove that, I just want paste in some mathed out randoms here.

    Here. Clean this up from my pasting skills, hit save, and you should see this update with random values every second. It's no longer creating values underneath it.