@title
Attribute Directives
@intro
Attribute directives attach behavior to elements.
@description
An **Attribute** directive changes the appearance or behavior of a DOM element.
# Contents
* [Directives overview](#directive-overview)
* [Build a simple attribute directive](#write-directive)
* [Apply the attribute directive to an element in a template](#apply-directive)
* [Respond to user-initiated events](#respond-to-user)
* [Pass values into the directive with an _@Input_ data binding](#bindings)
* [Bind to a second property](#second-property)
Try the
After the @Directive
metadata comes the directive's controller class, called HighlightDirective
, which contains the logic for the directive.
`) element. In Angular terms, the `
` element is the attribute **host**.
Put the template in its own
file that looks like this:
` element. It created an instance of the `HighlightDirective` class and injected a reference to the `
` element into the directive's constructor which sets the `
` element's background style to yellow. {@a respond-to-user} ## Respond to user-initiated events Currently, `myHighlight` simply sets an element color. The directive could be more dynamic. It could detect when the user mouses into or out of the element and respond by setting or clearing the highlight color. Begin by adding `HostListener` to the list of imported symbols; add the `Input` symbol as well because you'll need it soon. {@example 'attribute-directives/ts/src/app/highlight.directive.ts' region='imports'} Then add two eventhandlers that respond when the mouse enters or leaves, each adorned by the `HostListener` !{_decorator}. {@example 'attribute-directives/ts/src/app/highlight.directive.2.ts' region='mouse-methods'} The `@HostListener` !{_decorator} lets you subscribe to events of the DOM element that hosts an attribute directive, the `
` in this case. Of course you could reach into the DOM with standard JavaScript and and attach event listeners manually. There are at least three problems with _that_ approach: 1. You have to write the listeners correctly. 1. The code must *detach* the listener when the directive is destroyed to avoid memory leaks. 1. Talking to DOM API directly isn't a best practice. The handlers delegate to a helper method that sets the color on the DOM element, `#{_priv}el`, which you declare and initialize in the constructor. {@example 'attribute-directives/ts/src/app/highlight.directive.2.ts' region='ctor'} Here's the updated directive in full: {@example 'attribute-directives/ts/src/app/highlight.directive.2.ts'} Run the app and confirm that the background color appears when the mouse hovers over the `p` and disappears as it moves out. {@a bindings} ## Pass values into the directive with an _@Input_ data binding Currently the highlight color is hard-coded _within_ the directive. That's inflexible. In this section, you give the developer the power to set the highlight color while applying the directive. Start by adding a `highlightColor` property to the directive class like this: {@example 'attribute-directives/ts/src/app/highlight.directive.2.ts' region='color'} {@a input} ### Binding to an _@Input_ property Notice the `@Input` !{_decorator}. It adds metadata to the class that makes the directive's `highlightColor` property available for binding. It's called an *input* property because data flows from the binding expression _into_ the directive. Without that input metadata, Angular rejects the binding; see [below](#why-input "Why add @Input?") for more about that. Try it by adding the following directive binding variations to the `AppComponent` template: {@example 'attribute-directives/ts/src/app/app.component.1.html' region='color-1'} Add a `color` property to the `AppComponent`. {@example 'attribute-directives/ts/src/app/app.component.1.ts' region='class'} Let it control the highlight color with a property binding. {@example 'attribute-directives/ts/src/app/app.component.1.html' region='color-2'} That's good, but it would be nice to _simultaneously_ apply the directive and set the color _in the same attribute_ like this. {@example 'attribute-directives/ts/src/app/app.component.html' region='color'} The `[myHighlight]` attribute binding both applies the highlighting directive to the `
` element
and sets the directive's highlight color with a property binding.
You're re-using the directive's attribute selector (`[myHighlight]`) to do both jobs.
That's a crisp, compact syntax.
You'll have to rename the directive's `highlightColor` property to `myHighlight` because that's now the color property binding name.
{@example 'attribute-directives/ts/src/app/highlight.directive.2.ts' region='color-2'}
This is disagreeable. The word, `myHighlight`, is a terrible property name and it doesn't convey the property's intent.
{@a input-alias}
### Bind to an _@Input_ alias
Fortunately you can name the directive property whatever you want _and_ **_alias it_** for binding purposes.
Restore the original property name and specify the selector as the alias in the argument to `@Input`.
_Inside_ the directive the property is known as `highlightColor`.
_Outside_ the directive, where you bind to it, it's known as `myHighlight`.
You get the best of both worlds: the property name you want and the binding syntax you want:
{@example 'attribute-directives/ts/src/app/app.component.html' region='color'}
Now that you're binding to `highlightColor`, modify the `onMouseEnter()` method to use it.
If someone neglects to bind to `highlightColor`, highlight in "red" by default.
{@example 'attribute-directives/ts/src/app/highlight.directive.3.ts' region='mouse-enter'}
Here's the latest version of the directive class.## Write a harness to try itIt may be difficult to imagine how this directive actually works.
In this section, you'll turn `AppComponent` into a harness that
lets you pick the highlight color with a radio button and bind your color choice to the directive.
Update `app.component.html` as follows:
Revise the `AppComponent.color` so that it has no initial value.Here is the harness and directive in action.
{@a second-property}
## Bind to a second property
This highlight directive has a single customizable property. In a real app, it may need more.
At the moment, the default color—the color that prevails until
the user picks a highlight color—is hard-coded as "red".
Let the template developer set the default color.
Add a second **input** property to `HighlightDirective` called `defaultColor`:Revise the directive's `onMouseEnter` so that it first tries to highlight with the `highlightColor`,
then with the `defaultColor`, and falls back to "red" if both properties are undefined.
{@example 'attribute-directives/ts/src/app/highlight.directive.ts' region='mouse-enter'}
How do you bind to a second property when you're already binding to the `myHighlight` attribute name?
As with components, you can add as many directive property bindings as you need by stringing them along in the template.
The developer should be able to write the following template HTML to both bind to the `AppComponent.color`
and fall back to "violet" as the default color.
{@example 'attribute-directives/ts/src/app/app.component.html' region='defaultColor'}
Angular knows that the `defaultColor` binding belongs to the `HighlightDirective`
because you made it _public_ with the `@Input` !{_decorator}.
Here's how the harness should work when you're done coding.
## Summary
This page covered how to:
- [Build an **attribute directive**](#write-directive) that modifies the behavior of an element.
- [Apply the directive](#apply-directive) to an element in a template.
- [Respond to **events**](#respond-to-user) that change the directive's behavior.
- [**Bind** values to the directive](#bindings).
The final source code follows: