2015-11-21 11:23:40 -08:00
|
|
|
// #docregion
|
|
|
|
import {Component} from 'angular2/core';
|
|
|
|
import {LoggerService} from './logger.service';
|
|
|
|
import {Spy} from './spy.directive';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'spy-parent',
|
|
|
|
template: `
|
|
|
|
<div class="parent">
|
|
|
|
<h2>Spy Directive</h2>
|
2016-01-23 18:21:09 +00:00
|
|
|
<p>
|
|
|
|
<input [(ngModel)]="newName"
|
|
|
|
(keyup.enter)="addHero()"
|
|
|
|
placeholder="Hero name">
|
|
|
|
<button (click)="addHero()">Add Hero</button>
|
|
|
|
<button (click)="reset()">Reset Heroes</button>
|
|
|
|
</p>` +
|
|
|
|
// #docregion template
|
|
|
|
`<div *ngFor="#hero of heroes" my-spy class="heroes">
|
|
|
|
{{hero}}
|
|
|
|
</div>`
|
|
|
|
// #enddocregion template
|
|
|
|
+ `<h4>-- Spy Lifecycle Hook Log --</h4>
|
2015-11-21 11:23:40 -08:00
|
|
|
<div *ngFor="#msg of spyLog">{{msg}}</div>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
styles: [
|
2016-01-23 18:21:09 +00:00
|
|
|
'.parent {background: khaki;}',
|
2015-11-21 11:23:40 -08:00
|
|
|
'.heroes {background: LightYellow; padding: 0 8px}'
|
|
|
|
],
|
|
|
|
directives: [Spy],
|
2016-01-23 18:21:09 +00:00
|
|
|
providers: [LoggerService]
|
2015-11-21 11:23:40 -08:00
|
|
|
})
|
|
|
|
export class SpyParentComponent {
|
|
|
|
newName = 'Herbie';
|
|
|
|
heroes:string[] = ['Windstorm', 'Magneta'];
|
|
|
|
spyLog:string[];
|
|
|
|
|
2016-01-23 18:21:09 +00:00
|
|
|
constructor(private _logger:LoggerService){
|
|
|
|
this.spyLog = _logger.logs;
|
2015-11-21 11:23:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
addHero() {
|
|
|
|
if (this.newName.trim()) {
|
|
|
|
this.heroes.push(this.newName.trim());
|
|
|
|
this.newName = '';
|
2016-01-23 18:21:09 +00:00
|
|
|
this._logger.tick();
|
2015-11-21 11:23:40 -08:00
|
|
|
}
|
|
|
|
}
|
2016-01-23 18:21:09 +00:00
|
|
|
removeHero(hero:string) {
|
|
|
|
this.heroes.splice(this.heroes.indexOf(hero), 1);
|
|
|
|
this._logger.tick();
|
|
|
|
}
|
2015-11-21 11:23:40 -08:00
|
|
|
reset(){
|
|
|
|
this._logger.log('-- reset --');
|
|
|
|
this.heroes.length = 0;
|
2016-01-23 18:21:09 +00:00
|
|
|
this._logger.tick();
|
2015-11-21 11:23:40 -08:00
|
|
|
}
|
|
|
|
}
|