Skip to content

Commit

Permalink
docs(docs-infra): improve the content of attribute directives (#44628)
Browse files Browse the repository at this point in the history
Improve docs "Understanding Angular-Directives-Attribute Directives"

In `highlight.directive.1.ts`, add the `private` keyword for `el` property
to make it correct and consistent with subsequent examples.

For section "Setting the value with user input", add a step to tell the
reader change the method `onMouseEnter`, thus more readable.

For section "Binding to a second property", delete the aliase for the
`Input` property, and changes the name back to `appHighlight` in the code
to make it consistent with the whole tutorial.

PR Close #44628
  • Loading branch information
shejialuo authored and atscott committed Jan 6, 2022
1 parent 3591032 commit 7f51f1d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Directive, ElementRef } from '@angular/core';
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor(el: ElementRef) {
el.nativeElement.style.backgroundColor = 'yellow';
constructor(private el: ElementRef) {
this.el.nativeElement.style.backgroundColor = 'yellow';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export class HighlightDirective {
@Input() appHighlight = '';
// #enddocregion input

// #docregion mouse-enter
@HostListener('mouseenter') onMouseEnter() {
this.highlight(this.appHighlight || 'red');
}
// #enddocregion mouse-enter

@HostListener('mouseleave') onMouseLeave() {
this.highlight('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export class HighlightDirective {
@Input() defaultColor = '';
// #enddocregion defaultColor

@Input('appHighlight') highlightColor = '';
@Input() appHighlight = '';

// #docregion mouse-enter
@HostListener('mouseenter') onMouseEnter() {
this.highlight(this.highlightColor || this.defaultColor || 'red');
this.highlight(this.appHighlight || this.defaultColor || 'red');
}
// #enddocregion mouse-enter

Expand Down
6 changes: 5 additions & 1 deletion aio/content/guide/attribute-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ This section guides you through adding radio buttons to bind your color choice t

<code-example path="attribute-directives/src/app/app.component.ts" header="src/app/app.component.ts (class)" region="class"></code-example>

1. In `highlight.directive.ts`, revise `onMouseEnter` method so that it first tries to highlight with `appHighlight` and falls back to `red` if `appHighlight` is `undefined`.

<code-example path="attribute-directives/src/app/highlight.directive.3.ts" header="src/app/highlight.directive.ts (mouse-enter)" region="mouse-enter"></code-example>

1. Serve your application to verify that the user can choose the color with the radio buttons.

<div class="lightbox">
Expand All @@ -136,7 +140,7 @@ This section guides you through configuring your application so the developer ca

<code-example path="attribute-directives/src/app/highlight.directive.ts" header="src/app/highlight.directive.ts (defaultColor)" region="defaultColor"></code-example>

1. 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`.
1. Revise the directive's `onMouseEnter` so that it first tries to highlight with the `appHighlight`, then with the `defaultColor`, and falls back to `red` if both properties are `undefined`.

<code-example path="attribute-directives/src/app/highlight.directive.ts" header="src/app/highlight.directive.ts (mouse-enter)" region="mouse-enter"></code-example>

Expand Down

0 comments on commit 7f51f1d

Please sign in to comment.