angular-cn/public/docs/_examples/attribute-directives/ts/app/highlight.directive.ts

47 lines
1.1 KiB
TypeScript
Raw Normal View History

// #docplaster
// #docregion full
import { Directive, ElementRef, Input } from '@angular/core';
@Directive({
selector: '[myHighlight]',
host: {
'(mouseenter)': 'onMouseEnter()',
'(mouseleave)': 'onMouseLeave()'
}
})
// #docregion class-1
export class HighlightDirective {
2016-03-26 12:18:13 -04:00
private _defaultColor = 'red';
private el: HTMLElement;
constructor(el: ElementRef) { this.el = el.nativeElement; }
// #enddocregion class-1
2016-03-26 12:18:13 -04:00
// #docregion defaultColor
2016-03-26 12:18:13 -04:00
@Input() set defaultColor(colorName:string){
this._defaultColor = colorName || this._defaultColor;
}
// #enddocregion defaultColor
// #docregion class-1
2016-03-26 12:18:13 -04:00
// #docregion color
@Input('myHighlight') highlightColor: string;
// #enddocregion color
// #docregion mouse-enter
onMouseEnter() { this.highlight(this.highlightColor || this._defaultColor); }
// #enddocregion mouse-enter
onMouseLeave() { this.highlight(null); }
private highlight(color:string) {
this.el.style.backgroundColor = color;
}
}
// #enddocregion class-1
// #enddocregion full
/*
// #docregion highlight
@Input() myHighlight: string;
// #enddocregion highlight
*/