2016-05-06 10:42:01 -04:00
block includes
include ../_util-fns
2015-11-19 19:59:22 -05:00
:marked
An **Attribute** directive changes the appearance or behavior of a DOM element.
2016-10-13 18:42:23 -04:00
2015-11-19 19:59:22 -05:00
:marked
2016-10-13 18:42:23 -04:00
# 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)
2016-12-19 20:12:42 -05:00
* [Pass values into the directive with an _@Input_ data binding](#bindings)
2016-10-13 18:42:23 -04:00
* [Bind to a second property](#second-property)
2016-05-20 19:18:58 -04:00
2016-12-19 20:12:42 -05:00
Try the <live-example title="Attribute Directive example"></live-example>.
2016-05-20 19:18:58 -04:00
2016-10-13 18:42:23 -04:00
.l-main-section
a#directive-overview
:marked
2015-11-19 19:59:22 -05:00
## Directives overview
2016-10-13 18:42:23 -04:00
2015-11-19 19:59:22 -05:00
There are three kinds of directives in Angular:
2017-02-20 21:05:10 -05:00
1. Components—directives with a template.
1. Structural directives—change the DOM layout by adding and removing DOM elements.
1. Attribute directives—change the appearance or behavior of an element, component, or another directive.
2016-10-13 18:42:23 -04:00
2016-11-21 20:13:21 -05:00
*Components* are the most common of the three directives.
2017-02-20 21:05:10 -05:00
You saw a component for the first time in the [QuickStart](../quickstart.html) guide.
2016-10-13 18:42:23 -04:00
2017-02-20 18:29:42 -05:00
*Structural Directives* change the structure of the view.
2017-02-06 22:06:13 -05:00
Two examples are [NgFor](template-syntax.html#ngFor) and [NgIf](template-syntax.html#ngIf).
Learn about them in the [Structural Directives](structural-directives.html) guide.
2016-10-13 18:42:23 -04:00
2017-02-20 18:29:42 -05:00
*Attribute directives* are used as attributes of elements.
2017-02-06 22:06:13 -05:00
The built-in [NgStyle](template-syntax.html#ngStyle) directive in the [Template Syntax](template-syntax.html) guide, for example,
2015-11-19 19:59:22 -05:00
can change several element styles at the same time.
2016-10-13 18:42:23 -04:00
.l-main-section
a#write-directive
:marked
## Build a simple attribute directive
An attribute directive minimally requires building a controller class annotated with
`@Directive`, which specifies the selector that identifies
the attribute.
The controller class implements the desired directive behavior.
2017-02-20 21:05:10 -05:00
This page demonstrates building a simple _myHighlight_ attribute
2016-10-13 18:42:23 -04:00
directive to set an element's background color
2017-02-20 21:05:10 -05:00
when the user hovers over that element. You can apply it like this:
2015-11-19 19:59:22 -05:00
2017-02-20 21:05:10 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.1.html','applied')
2015-11-19 19:59:22 -05:00
:marked
2016-10-13 18:42:23 -04:00
### Write the directive code
2017-02-20 21:05:10 -05:00
Follow the [setup](setup.html) instructions for creating a new local project
2016-11-21 20:13:21 -05:00
named <span ngio-ex>attribute-directives</span>.
2016-03-13 15:50:50 -04:00
:marked
2017-02-20 21:05:10 -05:00
Create the following source file in `src/app` with the following code:
2017-02-02 13:38:17 -05:00
+makeExample('src/app/highlight.directive.1.ts')
2015-11-19 19:59:22 -05:00
2016-05-06 10:42:01 -04:00
block highlight-directive-1
:marked
2016-10-13 18:42:23 -04:00
The `import` statement specifies symbols from the Angular `core`:
1. `Directive` provides the functionality of the `@Directive` decorator.
1. `ElementRef` [injects](dependency-injection.html) into the directive's constructor
so the code can access the DOM element.
1. `Input` allows data to flow from the binding expression into the directive.
Next, the `@Directive` decorator function contains the directive metadata in a configuration object
as an argument.
2016-05-06 10:42:01 -04:00
:marked
`@Directive` requires a CSS selector to identify
2016-10-13 18:42:23 -04:00
the HTML in the template that is associated with the directive.
2016-05-06 10:42:01 -04:00
The [CSS selector for an attribute](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors)
2015-11-19 19:59:22 -05:00
is the attribute name in square brackets.
2016-10-13 18:42:23 -04:00
Here, the directive's selector is `[myHighlight]`.
2017-02-20 21:05:10 -05:00
Angular locates all elements in the template that have an attribute named `myHighlight`.
2015-11-19 19:59:22 -05:00
.l-sub-section
:marked
### Why not call it "highlight"?
2016-10-13 18:42:23 -04:00
Though *highlight* is a more concise name than *myHighlight* and would work,
a best practice is to prefix selector names to ensure
they don't conflict with standard HTML attributes.
2017-02-20 21:05:10 -05:00
This also reduces the risk of colliding with third-party directive names.
2015-12-28 20:55:13 -05:00
2016-10-13 18:42:23 -04:00
Make sure you do **not** prefix the `highlight` directive name with **`ng`** because
that prefix is reserved for Angular and using it could cause bugs that are difficult to diagnose. For a simple demo, the short prefix, `my`, helps distinguish your custom directive.
2015-12-28 20:55:13 -05:00
2016-05-06 10:42:01 -04:00
p
2016-10-13 18:42:23 -04:00
| After the #[code @Directive] metadata comes the directive's controller class, called #[code HighlightDirective], which contains the logic for the directive.
2016-05-06 10:42:01 -04:00
+ifDocsFor('ts')
2016-10-13 18:42:23 -04:00
| Exporting #[code HighlightDirective] makes it accessible to other components.
2015-11-19 19:59:22 -05:00
:marked
Angular creates a new instance of the directive's controller class for
2016-12-02 19:54:27 -05:00
each matching element, injecting an Angular `ElementRef`
2016-01-26 16:42:17 -05:00
into the constructor.
2016-10-13 18:42:23 -04:00
`ElementRef` is a service that grants direct access to the DOM element
2016-12-02 19:54:27 -05:00
through its `nativeElement` property.
2015-11-19 19:59:22 -05:00
.l-main-section
2016-05-06 10:42:01 -04:00
a#apply-directive
2015-11-19 19:59:22 -05:00
:marked
## Apply the attribute directive
2016-10-13 18:42:23 -04:00
To use the new `HighlightDirective`, create a template that
2016-12-19 20:12:42 -05:00
applies the directive as an attribute to a paragraph (`<p>`) element.
2017-02-20 21:05:10 -05:00
In Angular terms, the `<p>` element is the attribute **host**.
2016-05-06 10:42:01 -04:00
p
2016-10-13 18:42:23 -04:00
| Put the template in its own
2016-05-06 10:42:01 -04:00
code #[+adjExPath('app.component.html')]
| file that looks like this:
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.1.html',null,'src/app/app.component.html')(format=".")
2015-11-19 19:59:22 -05:00
:marked
2016-10-13 18:42:23 -04:00
Now reference this template in the `AppComponent`:
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.ts',null,'src/app/app.component.ts')
2015-11-19 19:59:22 -05:00
:marked
2016-10-13 18:42:23 -04:00
Next, add an `import` statement to fetch the `Highlight` directive and
add that class to the `declarations` NgModule metadata. This way Angular
recognizes the directive when it encounters `myHighlight` in the template.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.module.ts',null,'src/app/app.module.ts')
2016-08-09 12:38:25 -04:00
:marked
2016-10-13 18:42:23 -04:00
Now when the app runs, the `myHighlight` directive highlights the paragraph text.
2015-11-19 19:59:22 -05:00
figure.image-display
img(src="/resources/images/devguide/attribute-directives/first-highlight.png" alt="First Highlight")
2016-01-26 16:42:17 -05:00
.l-sub-section
:marked
2016-10-13 18:42:23 -04:00
### Your directive isn't working?
2016-01-26 16:42:17 -05:00
2016-12-19 23:22:38 -05:00
Did you remember to add the directive to the `declarations` attribute of `@NgModule`? It is easy to forget!
2016-01-26 16:42:17 -05:00
Open the console in the browser tools and look for an error like this:
2016-05-06 10:42:01 -04:00
code-example(format="nocode").
2016-01-26 16:42:17 -05:00
EXCEPTION: Template parse errors:
2016-08-09 12:38:25 -04:00
Can't bind to 'myHighlight' since it isn't a known property of 'p'.
2016-10-13 18:42:23 -04:00
2016-01-26 16:42:17 -05:00
:marked
2016-12-19 20:12:42 -05:00
Angular detects that you're trying to bind to *something* but it can't find this directive
2017-02-20 18:29:42 -05:00
in the module's `declarations` array.
After specifying `HighlightDirective` in the `declarations` array,
2016-12-19 20:12:42 -05:00
Angular knows it can apply the directive to components declared in this module.
2016-10-13 18:42:23 -04:00
2015-11-19 19:59:22 -05:00
:marked
2017-02-20 18:29:42 -05:00
To summarize, Angular found the `myHighlight` attribute on the `<p>` element.
2016-12-19 20:12:42 -05:00
It created an instance of the `HighlightDirective` class and
injected a reference to the `<p>` element into the directive's constructor
which sets the `<p>` element's background style to yellow.
2015-11-19 19:59:22 -05:00
.l-main-section
2016-05-06 10:42:01 -04:00
a#respond-to-user
2015-11-19 19:59:22 -05:00
:marked
2016-10-13 18:42:23 -04:00
## Respond to user-initiated events
2015-11-19 19:59:22 -05:00
2016-10-13 18:42:23 -04:00
Currently, `myHighlight` simply sets an element color.
2016-12-19 20:12:42 -05:00
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.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
Begin by adding `HostListener` to the list of imported symbols;
2017-02-08 16:02:18 -05:00
add the `Input` symbol as well because you'll need it soon.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/highlight.directive.ts','imports')(format=".")
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
:marked
Then add two eventhandlers that respond when the mouse enters or leaves, each adorned by the `HostListener` !{_decorator}.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/highlight.directive.2.ts','mouse-methods')(format=".")
2016-05-06 10:42:01 -04:00
2016-12-19 20:12:42 -05:00
:marked
The `@HostListener` !{_decorator} lets you subscribe to events of the DOM element that hosts an attribute directive, the `<p>` in this case.
2016-05-06 10:42:01 -04:00
2015-11-19 19:59:22 -05:00
.l-sub-section
:marked
2016-12-19 20:12:42 -05:00
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:
2015-11-19 19:59:22 -05:00
2016-10-13 18:42:23 -04:00
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.
2015-11-19 19:59:22 -05:00
:marked
2016-12-19 20:12:42 -05:00
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.
2016-03-26 12:18:13 -04:00
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/highlight.directive.2.ts','ctor')(format=".")
2015-11-19 19:59:22 -05:00
:marked
2016-12-19 20:12:42 -05:00
Here's the updated directive in full:
2017-02-02 13:38:17 -05:00
+makeExample('src/app/highlight.directive.2.ts')
2015-11-19 19:59:22 -05:00
:marked
2016-10-13 18:42:23 -04:00
Run the app and confirm that the background color appears when the mouse hovers over the `p` and
disappears as it moves out.
2015-11-19 19:59:22 -05:00
figure.image-display
img(src="/resources/images/devguide/attribute-directives/highlight-directive-anim.gif" alt="Second Highlight")
.l-main-section
2016-05-06 10:42:01 -04:00
a#bindings
2015-11-19 19:59:22 -05:00
:marked
2016-12-19 20:12:42 -05:00
## Pass values into the directive with an _@Input_ data binding
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
Currently the highlight color is hard-coded _within_ the directive. That's inflexible.
2017-02-20 21:05:10 -05:00
In this section, you give the developer the power to set the highlight color while applying the directive.
2016-12-19 20:12:42 -05:00
Start by adding a `highlightColor` property to the directive class like this:
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/highlight.directive.2.ts','color', 'src/app/highlight.directive.ts')
2015-11-19 19:59:22 -05:00
2016-05-06 10:42:01 -04:00
a#input
2015-11-19 19:59:22 -05:00
:marked
2016-12-19 20:12:42 -05:00
### 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.
2017-02-20 18:29:42 -05:00
It's called an *input* property because data flows from the binding expression _into_ the directive.
2016-12-19 20:12:42 -05:00
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:
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.1.html','color-1', 'src/app/app.component.html')(format='.')
2016-12-19 20:12:42 -05:00
:marked
Add a `color` property to the `AppComponent`.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.1.ts','class', 'src/app/app.component.ts (class)')(format='.')
2015-11-19 19:59:22 -05:00
:marked
2016-12-19 20:12:42 -05:00
Let it control the highlight color with a property binding.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.1.html','color-2', 'src/app/app.component.html')
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
:marked
That's good, but it would be nice to _simultaneously_ apply the directive and set the color _in the same attribute_ like this.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.html','color')
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
:marked
2017-02-20 18:29:42 -05:00
The `[myHighlight]` attribute binding both applies the highlighting directive to the `<p>` element
2016-12-19 20:12:42 -05:00
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.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
You'll have to rename the directive's `highlightColor` property to `myHighlight` because that's now the color property binding name.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/highlight.directive.2.ts','color-2', 'src/app/highlight.directive.ts (renamed to match directive selector)')
2015-11-19 19:59:22 -05:00
:marked
2016-12-19 20:12:42 -05:00
This is disagreeable. The word, `myHighlight`, is a terrible property name and it doesn't convey the property's intent.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
a#input-alias
2015-11-19 19:59:22 -05:00
:marked
2016-12-19 20:12:42 -05:00
### Bind to an _@Input_ alias
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
Fortunately you can name the directive property whatever you want _and_ **_alias it_** for binding purposes.
2017-02-20 18:29:42 -05:00
Restore the original property name and specify the selector as the alias in the argument to `@Input`.
2015-11-19 19:59:22 -05:00
2017-02-02 13:38:17 -05:00
+makeExcerpt('src/app/highlight.directive.ts', 'color', 'src/app/highlight.directive.ts (color property with alias')
2016-12-19 20:12:42 -05:00
:marked
_Inside_ the directive the property is known as `highlightColor`.
_Outside_ the directive, where you bind to it, it's known as `myHighlight`.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
You get the best of both worlds: the property name you want and the binding syntax you want:
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.html','color')
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
:marked
2017-02-20 18:29:42 -05:00
Now that you're binding to `highlightColor`, modify the `onMouseEnter()` method to use it.
2016-12-19 20:12:42 -05:00
If someone neglects to bind to `highlightColor`, highlight in "red" by default.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/highlight.directive.3.ts', 'mouse-enter', 'src/app/highlight.directive.ts (mouse enter)')(format='.')
2016-12-19 20:12:42 -05:00
:marked
Here's the latest version of the directive class.
2017-02-02 13:38:17 -05:00
+makeExcerpt('src/app/highlight.directive.3.ts')
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
:marked
## Write a harness to try it
:marked
It 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.
2017-02-20 18:29:42 -05:00
2016-12-19 20:12:42 -05:00
Update `app.component.html` as follows:
2016-10-13 18:42:23 -04:00
2017-02-02 13:38:17 -05:00
+makeExcerpt('attribute-directives/ts/src/app/app.component.html', 'v2', '')
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
:marked
Revise the `AppComponent.color` so that it has no initial value.
2017-02-02 13:38:17 -05:00
+makeExcerpt('attribute-directives/ts/src/app/app.component.ts', 'class', '')
2015-11-19 19:59:22 -05:00
:marked
2016-12-19 20:12:42 -05:00
Here is the harness and directive in action.
2015-11-19 19:59:22 -05:00
figure.image-display
2015-12-03 15:41:58 -05:00
img(src="/resources/images/devguide/attribute-directives/highlight-directive-v2-anim.gif" alt="Highlight v.2")
.l-main-section
2016-10-13 18:42:23 -04:00
a#second-property
2015-12-03 15:41:58 -05:00
:marked
## Bind to a second property
2016-12-19 20:12:42 -05:00
This highlight directive has a single customizable property. In a real app, it may need more.
2016-10-13 18:42:23 -04:00
2017-02-20 21:05:10 -05:00
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.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
Add a second **input** property to `HighlightDirective` called `defaultColor`:
2017-02-02 13:38:17 -05:00
+makeExcerpt('attribute-directives/ts/src/app/highlight.directive.ts', 'defaultColor','src/app/highlight.directive.ts (defaultColor)')
2015-12-16 22:38:07 -05:00
:marked
2016-12-19 20:12:42 -05:00
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.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/highlight.directive.ts', 'mouse-enter')(format=".")
2015-11-19 19:59:22 -05:00
:marked
2016-12-19 20:12:42 -05:00
How do you bind to a second property when you're already binding to the `myHighlight` attribute name?
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
As with components, you can add as many directive property bindings as you need by stringing them along in the template.
2017-02-20 18:29:42 -05:00
The developer should be able to write the following template HTML to both bind to the `AppComponent.color`
2016-12-19 20:12:42 -05:00
and fall back to "violet" as the default color.
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.html', 'defaultColor')(format=".")
2016-12-19 20:12:42 -05:00
:marked
2017-02-20 18:29:42 -05:00
Angular knows that the `defaultColor` binding belongs to the `HighlightDirective`
2016-12-19 20:12:42 -05:00
because you made it _public_ with the `@Input` !{_decorator}.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
Here's how the harness should work when you're done coding.
2015-12-03 15:41:58 -05:00
figure.image-display
img(src="/resources/images/devguide/attribute-directives/highlight-directive-final-anim.gif" alt="Final Highlight")
2015-11-19 19:59:22 -05:00
.l-main-section
:marked
## Summary
2016-10-13 18:42:23 -04:00
This page covered how to:
2016-12-19 20:12:42 -05:00
- [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).
2015-11-19 19:59:22 -05:00
2016-12-19 20:12:42 -05:00
The final source code follows:
2015-11-19 19:59:22 -05:00
+makeTabs(
2017-02-02 13:38:17 -05:00
`attribute-directives/ts/src/app/app.component.ts,
attribute-directives/ts/src/app/app.component.html,
attribute-directives/ts/src/app/highlight.directive.ts,
attribute-directives/ts/src/app/app.module.ts,
attribute-directives/ts/src/main.ts,
attribute-directives/ts/src/index.html
2015-11-19 19:59:22 -05:00
`,
2016-12-19 20:12:42 -05:00
'',
2017-02-02 13:38:17 -05:00
`app/app.component.ts,
app/app.component.html,
app/highlight.directive.ts,
app/app.module.ts,
2016-01-26 16:42:17 -05:00
main.ts,
2015-11-19 19:59:22 -05:00
index.html
`)
2015-12-16 22:38:07 -05:00
2016-12-19 20:12:42 -05:00
:marked
You can also experience and download the <live-example title="Attribute Directive example"></live-example>.
2015-12-16 22:38:07 -05:00
2016-05-06 10:42:01 -04:00
a#why-input
2015-12-16 22:38:07 -05:00
.l-main-section
:marked
2016-12-19 20:12:42 -05:00
### Appendix: Why add _@Input_?
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
In this demo, the `hightlightColor` property is an ***input*** property of
the `HighlightDirective`. You've seen it applied without an alias:
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/highlight.directive.2.ts','color')
2016-12-19 20:12:42 -05:00
:marked
You've seen it with an alias:
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/highlight.directive.ts','color')
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
:marked
2017-02-20 18:29:42 -05:00
Either way, the `@Input` !{_decorator} tells Angular that this property is
2016-12-19 20:12:42 -05:00
_public_ and available for binding by a parent component.
Without `@Input`, Angular refuses to bind to the property.
2016-10-13 18:42:23 -04:00
2017-02-20 18:29:42 -05:00
You've bound template HTML to component properties before and never used `@Input`.
2016-12-19 20:12:42 -05:00
What's different?
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
The difference is a matter of trust.
Angular treats a component's template as _belonging_ to the component.
The component and its template trust each other implicitly.
Therefore, the component's own template may bind to _any_ property of that component,
with or without the `@Input` !{_decorator}.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
But a component or directive shouldn't blindly trust _other_ components and directives.
The properties of a component or directive are hidden from binding by default.
They are _private_ from an Angular binding perspective.
2017-02-20 18:29:42 -05:00
When adorned with the `@Input` !{_decorator}, the property becomes _public_ from an Angular binding perspective.
2016-12-19 20:12:42 -05:00
Only then can it be bound by some other component or directive.
2017-02-20 18:29:42 -05:00
2016-12-19 20:12:42 -05:00
You can tell if `@Input` is needed by the position of the property name in a binding.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
* When it appears in the template expression to the ***right*** of the equals (=),
it belongs to the template's component and does not require the `@Input` !{_decorator}.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
* When it appears in **square brackets** ([ ]) to the **left** of the equals (=),
the property belongs to some _other_ component or directive;
that property must be adorned with the `@Input` !{_decorator}.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
Now apply that reasoning to the following example:
2017-02-02 13:38:17 -05:00
+makeExample('attribute-directives/ts/src/app/app.component.html','color')(format=".")
2016-12-19 20:12:42 -05:00
:marked
2017-02-20 18:29:42 -05:00
* The `color` property in the expression on the right belongs to the template's component.
2016-12-19 20:12:42 -05:00
The template and its component trust each other.
The `color` property doesn't require the `@Input` !{_decorator}.
2016-10-13 18:42:23 -04:00
2016-12-19 20:12:42 -05:00
* The `myHighlight` property on the left refers to an _aliased_ property of the `MyHighlightDirective`,
not a property of the template's component. There are trust issues.
Therefore, the directive property must carry the `@Input` !{_decorator}.