docs: make the `spy-directive` docregion (in `lifecycle-hooks` example) easier to follow (#40208)

Previously, the docregion code referenced a `nextId` variable that was
not shown in the code, which was confusing for the reader.

This commit makes the declaration of the `nextId` variable part of the
docregion, so it is clear to the reader where it comes from and how it
is initialized.
This commit also removes the `logIt()` helper method, which didn't seem
to add value and calls `logger.log()` directly instead.

PR Close #40208
This commit is contained in:
George Kalpakas 2020-12-21 17:23:59 +02:00 committed by Joey Perrott
parent 251b27701f
commit e28d460307
1 changed files with 6 additions and 6 deletions

View File

@ -3,9 +3,9 @@ import { Directive, OnInit, OnDestroy } from '@angular/core';
import { LoggerService } from './logger.service'; import { LoggerService } from './logger.service';
// #docregion spy-directive
let nextId = 1; let nextId = 1;
// #docregion spy-directive
// Spy on any element to which it is applied. // Spy on any element to which it is applied.
// Usage: <div appSpy>...</div> // Usage: <div appSpy>...</div>
@Directive({selector: '[appSpy]'}) @Directive({selector: '[appSpy]'})
@ -13,12 +13,12 @@ export class SpyDirective implements OnInit, OnDestroy {
constructor(private logger: LoggerService) { } constructor(private logger: LoggerService) { }
ngOnInit() { this.logIt(`onInit`); } ngOnInit() {
this.logger.log(`Spy #${nextId++} onInit`);
}
ngOnDestroy() { this.logIt(`onDestroy`); } ngOnDestroy() {
this.logger.log(`Spy #${nextId++} onDestroy`);
private logIt(msg: string) {
this.logger.log(`Spy #${nextId++} ${msg}`);
} }
} }
// #enddocregion spy-directive // #enddocregion spy-directive