angular-cn/public/docs/_examples/lifecycle-hooks/ts/app/spy.directive.ts

25 lines
584 B
TypeScript

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