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

29 lines
605 B
TypeScript
Raw Normal View History

2016-03-26 12:18:13 -04:00
// #docplaster
// #docregion
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;
2016-03-26 12:18:13 -04:00
constructor(el: ElementRef) {
this.el = el.nativeElement;
2016-03-26 12:18:13 -04:00
}
onMouseEnter() { this.highlight(this.highlightColor || 'cyan'); }
onMouseLeave() { this.highlight(null); }
2016-03-26 12:18:13 -04:00
private highlight(color: string) {
this.el.style.backgroundColor = color;
2016-03-26 12:18:13 -04:00
}
}