Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit e5b11d4

Browse files
chalinkwalrath
authored andcommitted
docs(guide/attribute-directives): follow-up to #1654 (#1765)
- Updated Dart code to match TS. - Ran dartfmt. - Enabled e2e tests; suites passed: - public/docs/_examples/attribute-directives/dart - public/docs/_examples/attribute-directives/ts - Prose copyedits.
1 parent f8e6b5d commit e5b11d4

File tree

6 files changed

+49
-43
lines changed

6 files changed

+49
-43
lines changed

public/docs/_examples/attribute-directives/dart/example-config.json

Whitespace-only changes.

public/docs/_examples/attribute-directives/dart/lib/highlight_directive.dart

+16-14
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,41 @@
22
// #docregion full
33
import 'package:angular2/core.dart';
44

5-
@Directive(selector: '[myHighlight]', host: const {
6-
'(mouseenter)': 'onMouseEnter()',
7-
'(mouseleave)': 'onMouseLeave()'
8-
})
9-
// #docregion class-1
5+
@Directive(selector: '[myHighlight]')
6+
// #docregion class
107
class HighlightDirective {
118
String _defaultColor = 'red';
129
final dynamic _el;
1310

1411
HighlightDirective(ElementRef elRef) : _el = elRef.nativeElement;
15-
// #enddocregion class-1
12+
// #enddocregion class
1613

1714
// #docregion defaultColor
18-
@Input() set defaultColor(String colorName) {
15+
@Input()
16+
set defaultColor(String colorName) {
1917
_defaultColor = (colorName ?? _defaultColor);
2018
}
2119
// #enddocregion defaultColor
22-
// #docregion class-1
20+
// #docregion class
2321

2422
// #docregion color
25-
@Input('myHighlight') String highlightColor;
23+
@Input('myHighlight')
24+
String highlightColor;
2625
// #enddocregion color
27-
26+
2827
// #docregion mouse-enter
29-
void onMouseEnter() { _highlight(highlightColor ?? _defaultColor); }
28+
@HostListener('mouseenter')
29+
void onMouseEnter() => _highlight(highlightColor ?? _defaultColor);
30+
3031
// #enddocregion mouse-enter
31-
void onMouseLeave() { _highlight(); }
32+
@HostListener('mouseleave')
33+
void onMouseLeave() => _highlight();
3234

3335
void _highlight([String color]) {
34-
if(_el != null) _el.style.backgroundColor = color;
36+
if (_el != null) _el.style.backgroundColor = color;
3537
}
3638
}
37-
// #enddocregion class-1
39+
// #enddocregion class
3840
// #enddocregion full
3941
/*
4042
// #docregion highlight

public/docs/_examples/attribute-directives/dart/lib/highlight_directive_2.dart

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
// #docregion
22
import 'package:angular2/core.dart';
33

4-
@Directive(selector: '[myHighlight]',
5-
// #docregion host
6-
host: const {
7-
'(mouseenter)': 'onMouseEnter()',
8-
'(mouseleave)': 'onMouseLeave()'
9-
}
10-
// #enddocregion host
11-
)
4+
@Directive(selector: '[myHighlight]')
125
class HighlightDirective {
136
// #docregion ctor
147
final dynamic _el;
158

169
HighlightDirective(ElementRef elRef) : _el = elRef.nativeElement;
1710
// #enddocregion ctor
1811

19-
// #docregion mouse-methods
20-
void onMouseEnter() { _highlight("yellow"); }
21-
void onMouseLeave() { _highlight(); }
12+
// #docregion mouse-methods, host
13+
@HostListener('mouseenter')
14+
void onMouseEnter() {
15+
// #enddocregion host
16+
_highlight('yellow');
17+
// #docregion host
18+
}
19+
20+
@HostListener('mouseleave')
21+
void onMouseLeave() {
22+
// #enddocregion host
23+
_highlight();
24+
// #docregion host
25+
}
26+
// #enddocregion host
2227

2328
void _highlight([String color]) {
2429
if (_el != null) _el.style.backgroundColor = color;

public/docs/_examples/attribute-directives/ts/app/highlight.directive.2.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* tslint:disable:no-unused-variable */
2-
// #docplaster
32
// #docregion
43
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
54

@@ -8,9 +7,9 @@ import { Directive, ElementRef, HostListener, Input } from '@angular/core';
87
})
98

109
export class HighlightDirective {
11-
1210
// #docregion ctor
1311
private el: HTMLElement;
12+
1413
constructor(el: ElementRef) { this.el = el.nativeElement; }
1514
// #enddocregion ctor
1615

public/docs/_examples/attribute-directives/ts/app/highlight.directive.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import { Directive, ElementRef, HostListener, Input } from '@angular/core';
55
@Directive({
66
selector: '[myHighlight]'
77
})
8-
// #docregion class-1
8+
// #docregion class
99
export class HighlightDirective {
1010
private _defaultColor = 'red';
1111
private el: HTMLElement;
1212

1313
constructor(el: ElementRef) { this.el = el.nativeElement; }
14-
// #enddocregion class-1
14+
// #enddocregion class
1515

1616
// #docregion defaultColor
1717
@Input() set defaultColor(colorName: string){
1818
this._defaultColor = colorName || this._defaultColor;
1919
}
2020
// #enddocregion defaultColor
21-
// #docregion class-1
21+
// #docregion class
2222

2323
// #docregion color
2424
@Input('myHighlight') highlightColor: string;
@@ -37,7 +37,7 @@ export class HighlightDirective {
3737
this.el.style.backgroundColor = color;
3838
}
3939
}
40-
// #enddocregion class-1
40+
// #enddocregion class
4141
// #enddocregion full
4242
/*
4343
// #docregion highlight

public/docs/ts/latest/guide/attribute-directives.jade

+12-12
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ a#write-directive
6464
include ../_quickstart_repo
6565
:marked
6666
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')
6868

6969
block highlight-directive-1
7070
:marked
@@ -97,7 +97,7 @@ block highlight-directive-1
9797

9898
We need a prefix of our own, preferably short, and `my` will do for now.
9999
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.
101101
+ifDocsFor('ts')
102102
| We export `HighlightDirective` to make it accessible to other components.
103103
:marked
@@ -169,13 +169,13 @@ a#respond-to-user
169169
1. detect when the user hovers into and out of the element,
170170
2. respond to those actions by setting and clearing the highlight color, respectively.
171171

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.
173173

174174
+makeExample('attribute-directives/ts/app/highlight.directive.2.ts','host')(format=".")
175175

176176
.l-sub-section
177177
: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.
179179

180180
We could have attached event listeners by manipulating the host DOM element directly, but
181181
there are at least three problems with such an approach:
@@ -184,7 +184,7 @@ a#respond-to-user
184184
1. We must *detach* our listener when the directive is destroyed to avoid memory leaks.
185185
1. We'd be talking to DOM API directly which, we learned, is something to avoid.
186186

187-
Let's roll with the `@HostListener` decorator.
187+
Let's roll with the `@HostListener` !{_decorator}.
188188
:marked
189189
Now we implement the two mouse event handlers:
190190
+makeExample('attribute-directives/ts/app/highlight.directive.2.ts','mouse-methods')(format=".")
@@ -195,7 +195,7 @@ a#respond-to-user
195195
+makeExample('attribute-directives/ts/app/highlight.directive.2.ts','ctor')(format=".")
196196
:marked
197197
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')
199199
:marked
200200
We run the app and confirm that the background color appears as we move the mouse over the `p` and
201201
disappears as we move out.
@@ -213,12 +213,12 @@ a#bindings
213213
We'll extend our directive class with a bindable **input** `highlightColor` property and use it when we highlight text.
214214

215215
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')
217217
a#input
218218
:marked
219219
The new `highlightColor` property is called an *input* property because data flows from the binding expression into our directive.
220220
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')
222222
:marked
223223
`@Input` adds metadata to the class that makes the `highlightColor` property available for
224224
property binding under the `myHighlight` alias.
@@ -232,25 +232,25 @@ a#input
232232

233233
We could resolve the discrepancy by renaming the property to `myHighlight` and define it as follows:
234234

235-
+makeExample('attribute-directives/ts/app/highlight.directive.ts', 'highlight')
235+
+makeExcerpt('app/highlight.directive.ts', 'highlight', '')
236236
:marked
237237
Maybe we don't want that property name inside the directive perhaps because it
238238
doesn't express our intention well.
239239
We can **alias** the `highlightColor` property with the attribute name by
240240
passing `myHighlight` into the `@Input` #{_decorator}:
241-
+makeExample('attribute-directives/ts/app/highlight.directive.ts', 'color')
241+
+makeExcerpt('app/highlight.directive.ts', 'color', '')
242242
:marked
243243
Now that we're getting the highlight color as an input, we modify the `onMouseEnter()` method to use
244244
it instead of the hard-coded color name.
245245
We also define red as the default color to fallback on in case
246246
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', '')
248248
:marked
249249
Now we'll update our `AppComponent` template to let
250250
users pick the highlight color and bind their choice to our directive.
251251

252252
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', '')
254254

255255
.l-sub-section
256256
:marked

0 commit comments

Comments
 (0)