Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 833 of the free egghead.io lessons, plus get Angular 2 content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Assign a Structual Directive a Dynamic Context in Angular 2

3:01 Angular 2 lesson by

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.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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.

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.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?