Angular 2 - First Directive (es5)

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

Directives in Angular 2 are basically Components without templates. Directives allow you to interact with other components. In this video, we'll use Directive's hostListeners and Directives hostProperties to set up those relationships.

[00:00] Directives in Angular 2 are basically components without templates or view annotations. Or you could think a component is a directive with a view annotation. Either way you want to think about it is fine with me. Let's go ahead and create our first directive here. We'll call him "clicky." Then this directive also requires annotations just like our component did, and instead of new Angular component annotation we'll do new Angular directive annotation.

[00:32] And our selector is going to be brackets with clicky inside of it, so brackets mean it's an attribute of clicky. Let's keep checking if this is working by saying, "Is this thing on?" Then we just need to add clicky to our list of directives inside of our component, as well as add the clicky attributes somewhere on our component template, so I'll hit save, and you can see that yes, this thing is on.

[01:00] Now the way you interact between a directive and a component is by treating the component as a host. So if you look at clicky, you can see that this H1 is the host of this clicky attribute, so anything that's on this H1 will be considered a host listener or a host property. To set up a host listener, we'll say host.listeners and this is just an object where we say something like click is the name of the event we want to listen to, and then onClick is how we'll handle it.

[01:32] So we can just handle onClick inside of here, so onClick. Then we'll just log out, "You clicked my host." So hit save, come over here, click on our app, and you can see that you clicked my host. Now for properties I can just say host properties which is also an object, and the property we're going to grab is just the text content which will just allow us to change that inner text of this H1. So instead of logging this out, I can say, this text content is equal to "Is this thing on?"

[02:10] When I click on it, I can say this.textcontent You clicked my host." I'll hit save, come over here you can see it already says is this thing on? Come and click on it, and it changes to "You clicked my host."

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today