angular-cn/public/docs/_examples/cb-dependency-injection/ts/app/highlight.directive.ts

29 lines
609 B
TypeScript
Raw Normal View History

2016-03-26 12:18:13 -04:00
// #docplaster
// #docregion
2016-04-27 14:28:22 -04:00
import {Directive, ElementRef, Input} from '@angular/core';
2016-03-26 12:18:13 -04:00
@Directive({
selector: '[myHighlight]',
host: {
'(mouseenter)': 'onMouseEnter()',
'(mouseleave)': 'onMouseLeave()'
}
})
export class HighlightDirective {
@Input('myHighlight') highlightColor: string;
private _el: HTMLElement;
constructor(el: ElementRef) {
this._el = el.nativeElement;
}
onMouseEnter() { this._highlight(this.highlightColor || 'cyan'); }
onMouseLeave() { this._highlight(null); }
private _highlight(color: string) {
this._el.style.backgroundColor = color;
}
}