@@ -64,7 +64,7 @@ a#write-directive
64
64
include ../_quickstart_repo
65
65
:marked
66
66
Create the following source file in the indicated folder with the given code:
67
- + makeExample('attribute-directives/ts/ app/highlight.directive.1.ts' , null , 'app/highlight.directive .ts' )
67
+ + makeExample('app/highlight.directive.1.ts' )
68
68
69
69
block highlight-directive-1
70
70
:marked
@@ -97,7 +97,7 @@ block highlight-directive-1
97
97
98
98
We need a prefix of our own, preferably short, and `my` will do for now.
99
99
p
100
- | After the ` @Directive` metadata comes the directive's controller class, which contains the logic for the directive.
100
+ | After the #[ code @Directive] metadata comes the directive's controller class, which contains the logic for the directive.
101
101
+ ifDocsFor('ts' )
102
102
| We export `HighlightDirective` to make it accessible to other components.
103
103
:marked
@@ -169,13 +169,13 @@ a#respond-to-user
169
169
1. detect when the user hovers into and out of the element,
170
170
2. respond to those actions by setting and clearing the highlight color, respectively.
171
171
172
- We use the `@HostListener` decorator on a method which is called when the event is raised.
172
+ We apply the `@HostListener` !{_decorator} to methods which are called when an event is raised.
173
173
174
174
+ makeExample('attribute-directives/ts/app/highlight.directive.2.ts' ,'host' )( format ="." )
175
175
176
176
.l-sub-section
177
177
:marked
178
- The `@HostListener` decorator refers to the DOM element that hosts our attribute directive, the `<p>` in our case.
178
+ The `@HostListener` !{_decorator} refers to the DOM element that hosts our attribute directive, the `<p>` in our case.
179
179
180
180
We could have attached event listeners by manipulating the host DOM element directly, but
181
181
there are at least three problems with such an approach:
@@ -184,7 +184,7 @@ a#respond-to-user
184
184
1. We must *detach* our listener when the directive is destroyed to avoid memory leaks.
185
185
1. We'd be talking to DOM API directly which, we learned, is something to avoid.
186
186
187
- Let's roll with the `@HostListener` decorator .
187
+ Let's roll with the `@HostListener` !{_decorator} .
188
188
:marked
189
189
Now we implement the two mouse event handlers:
190
190
+ makeExample('attribute-directives/ts/app/highlight.directive.2.ts' ,'mouse-methods' )( format ="." )
@@ -195,7 +195,7 @@ a#respond-to-user
195
195
+ makeExample('attribute-directives/ts/app/highlight.directive.2.ts' ,'ctor' )( format ="." )
196
196
:marked
197
197
Here's the updated directive:
198
- + makeExample('attribute-directives/ts/ app/highlight.directive.2.ts' , null , 'app/highlight.directive .ts' )
198
+ + makeExample('app/highlight.directive.2.ts' )
199
199
:marked
200
200
We run the app and confirm that the background color appears as we move the mouse over the `p` and
201
201
disappears as we move out.
@@ -213,12 +213,12 @@ a#bindings
213
213
We'll extend our directive class with a bindable **input** `highlightColor` property and use it when we highlight text.
214
214
215
215
Here is the final version of the class:
216
- + makeExample( 'attribute-directives/ts/ app/highlight.directive.ts' , 'class-1' , 'app/highlight.directive.ts (class only) ' )
216
+ + makeExcerpt( ' app/highlight.directive.ts' , 'class' )
217
217
a#input
218
218
:marked
219
219
The new `highlightColor` property is called an *input* property because data flows from the binding expression into our directive.
220
220
Notice the `@Input()` #{_decorator} applied to the property.
221
- + makeExample( 'attribute-directives/ts/ app/highlight.directive.ts' , 'color' )
221
+ + makeExcerpt( ' app/highlight.directive.ts' , 'color' )
222
222
:marked
223
223
`@Input` adds metadata to the class that makes the `highlightColor` property available for
224
224
property binding under the `myHighlight` alias.
@@ -232,25 +232,25 @@ a#input
232
232
233
233
We could resolve the discrepancy by renaming the property to `myHighlight` and define it as follows:
234
234
235
- + makeExample( 'attribute-directives/ts/ app/highlight.directive.ts' , 'highlight' )
235
+ + makeExcerpt( ' app/highlight.directive.ts' , 'highlight' , ' ' )
236
236
:marked
237
237
Maybe we don't want that property name inside the directive perhaps because it
238
238
doesn't express our intention well.
239
239
We can **alias** the `highlightColor` property with the attribute name by
240
240
passing `myHighlight` into the `@Input` #{_decorator}:
241
- + makeExample( 'attribute-directives/ts/ app/highlight.directive.ts' , 'color' )
241
+ + makeExcerpt( ' app/highlight.directive.ts' , 'color' , ' ' )
242
242
:marked
243
243
Now that we're getting the highlight color as an input, we modify the `onMouseEnter()` method to use
244
244
it instead of the hard-coded color name.
245
245
We also define red as the default color to fallback on in case
246
246
the user neglects to bind with a color.
247
- + makeExample ('attribute-directives/ts/app/highlight.directive.ts' , 'mouse-enter' )
247
+ + makeExcerpt ('attribute-directives/ts/app/highlight.directive.ts' , 'mouse-enter' , ' ' )
248
248
:marked
249
249
Now we'll update our `AppComponent` template to let
250
250
users pick the highlight color and bind their choice to our directive.
251
251
252
252
Here is the updated template:
253
- + makeExample ('attribute-directives/ts/app/app.component.html' , 'v2' )
253
+ + makeExcerpt ('attribute-directives/ts/app/app.component.html' , 'v2' , ' ' )
254
254
255
255
.l-sub-section
256
256
:marked
0 commit comments