Skip to content

Commit c93d021

Browse files
SkyZeroZxthePunderWoman
authored andcommitted
docs: replace @HostListener with host event bindings in attribute directives
1 parent 6d35ab8 commit c93d021

5 files changed

Lines changed: 32 additions & 18 deletions

File tree

adev/src/content/examples/attribute-directives/src/app/highlight.directive.2.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
// #docplaster
22
// #docregion imports
3-
import {Directive, ElementRef, HostListener, inject} from '@angular/core';
3+
import {Directive, ElementRef, inject} from '@angular/core';
44
// #enddocregion imports
55
// #docregion
66

7+
// #docregion decorator
78
@Directive({
89
selector: '[appHighlight]',
10+
host: {
11+
'(mouseenter)': 'onMouseEnter()',
12+
'(mouseleave)': 'onMouseLeave()',
13+
},
914
})
15+
// #enddocregion decorator
1016
export class HighlightDirective {
1117
private el = inject(ElementRef);
1218

1319
// #docregion mouse-methods
14-
@HostListener('mouseenter') onMouseEnter() {
20+
onMouseEnter() {
1521
this.highlight('yellow');
1622
}
1723

18-
@HostListener('mouseleave') onMouseLeave() {
24+
onMouseLeave() {
1925
this.highlight('');
2026
}
2127

adev/src/content/examples/attribute-directives/src/app/highlight.directive.3.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// #docregion, imports
2-
import {Directive, ElementRef, HostListener, inject, input} from '@angular/core';
2+
import {Directive, ElementRef, inject, input} from '@angular/core';
33
// #enddocregion imports
44

55
@Directive({
66
selector: '[appHighlight]',
7+
host: {
8+
'(mouseenter)': 'onMouseEnter()',
9+
'(mouseleave)': 'onMouseLeave()',
10+
},
711
})
812
export class HighlightDirective {
913
private el = inject(ElementRef);
@@ -13,12 +17,12 @@ export class HighlightDirective {
1317
// #enddocregion input
1418

1519
// #docregion mouse-enter
16-
@HostListener('mouseenter') onMouseEnter() {
20+
onMouseEnter() {
1721
this.highlight(this.appHighlight() || 'red');
1822
}
1923
// #enddocregion mouse-enter
2024

21-
@HostListener('mouseleave') onMouseLeave() {
25+
onMouseLeave() {
2226
this.highlight('');
2327
}
2428

adev/src/content/examples/attribute-directives/src/app/highlight.directive.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import {Directive, ElementRef, HostListener, inject, input} from '@angular/core';
1+
import {Directive, ElementRef, inject, input} from '@angular/core';
22

33
@Directive({
44
selector: '[appHighlight]',
5+
host: {
6+
'(mouseenter)': 'onMouseEnter()',
7+
'(mouseleave)': 'onMouseLeave()',
8+
},
59
})
610
export class HighlightDirective {
711
private el = inject(ElementRef);
@@ -13,12 +17,12 @@ export class HighlightDirective {
1317
appHighlight = input('');
1418

1519
// #docregion mouse-enter
16-
@HostListener('mouseenter') onMouseEnter() {
20+
onMouseEnter() {
1721
this.highlight(this.appHighlight() || this.defaultColor() || 'red');
1822
}
1923
// #enddocregion mouse-enter
2024

21-
@HostListener('mouseleave') onMouseLeave() {
25+
onMouseLeave() {
2226
this.highlight('');
2327
}
2428

adev/src/content/guide/components/host-elements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ the `host` property in the `@Component` decorator:
4848
'role': 'slider',
4949
'[attr.aria-valuenow]': 'value',
5050
'[class.active]': 'isActive()',
51-
'[style.background] : `hasError() ? 'red' : 'green'`,
51+
'[style.background]' : `hasError() ? 'red' : 'green'`,
5252
'[tabIndex]': 'disabled ? -1 : 0',
5353
'(keydown)': 'updateValue($event)',
5454
},

adev/src/content/guide/directives/attribute-directives.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ Angular creates an instance of the `HighlightDirective` class and injects a refe
4343

4444
This section shows you how to detect when a user mouses into or out of the element and to respond by setting or clearing the highlight color.
4545

46-
1. Import `HostListener` from '@angular/core'.
46+
1. Configure host event bindings using the `host` property in the `@Directive()` decorator.
4747

48-
<docs-code header="highlight.directive.ts (imports)" path="adev/src/content/examples/attribute-directives/src/app/highlight.directive.2.ts" visibleRegion="imports"/>
48+
<docs-code header="src/app/highlight.directive.ts (decorator)" path="adev/src/content/examples/attribute-directives/src/app/highlight.directive.2.ts" visibleRegion="decorator"/>
4949

50-
1. Add two event handlers that respond when the mouse enters or leaves, each with the `@HostListener()` decorator.
50+
1. Add two event handler methods, and map host element events to them via the `host` property.
5151

5252
<docs-code header="highlight.directive.ts (mouse-methods)" path="adev/src/content/examples/attribute-directives/src/app/highlight.directive.2.ts" visibleRegion="mouse-methods"/>
5353

54-
Subscribe to events of the DOM element that hosts an attribute directive, the `<p>` in this case, with the `@HostListener()` decorator.
54+
Subscribe to events of the DOM element that hosts an attribute directive (the `<p>` in this case) by configuring event listeners on the directive's [`host` property](guide/components/host-elements#binding-to-the-host-element).
5555

5656
HELPFUL: The handlers delegate to a helper method, `highlight()`, that sets the color on the host DOM element, `el`.
5757

@@ -71,17 +71,17 @@ This section walks you through setting the highlight color while applying the `H
7171

7272
<docs-code header="highlight.directive.ts (imports)" path="adev/src/content/examples/attribute-directives/src/app/highlight.directive.3.ts" visibleRegion="imports"/>
7373

74-
1. Add an `appHighlight` `input` property.
74+
2. Add an `appHighlight` `input` property.
7575

7676
<docs-code header="highlight.directive.ts" path="adev/src/content/examples/attribute-directives/src/app/highlight.directive.3.ts" visibleRegion="input"/>
7777

7878
The `input()` function adds metadata to the class that makes the directive's `appHighlight` property available for binding.
7979

80-
1. In `app.component.ts`, add a `color` property to the `AppComponent`.
80+
3. In `app.component.ts`, add a `color` property to the `AppComponent`.
8181

8282
<docs-code header="app.component.ts (class)" path="adev/src/content/examples/attribute-directives/src/app/app.component.1.ts" visibleRegion="class"/>
8383

84-
1. To simultaneously apply the directive and the color, use property binding with the `appHighlight` directive selector, setting it equal to `color`.
84+
4. To simultaneously apply the directive and the color, use property binding with the `appHighlight` directive selector, setting it equal to `color`.
8585

8686
<docs-code header="app.component.html (color)" path="adev/src/content/examples/attribute-directives/src/app/app.component.html" visibleRegion="color"/>
8787

@@ -113,7 +113,7 @@ This section guides you through adding radio buttons to bind your color choice t
113113

114114
This section guides you through configuring your application so the developer can set the default color.
115115

116-
1. Add a second `Input()` property to `HighlightDirective` called `defaultColor`.
116+
1. Add a second `input()` property to `HighlightDirective` called `defaultColor`.
117117

118118
<docs-code header="highlight.directive.ts (defaultColor)" path="adev/src/content/examples/attribute-directives/src/app/highlight.directive.ts" visibleRegion="defaultColor"/>
119119

0 commit comments

Comments
 (0)