Skip to content

Commit cb091ac

Browse files
author
lewis617
committed
hightlight
1 parent 9ed4769 commit cb091ac

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

directives/src/app.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
import {Component} from 'angular2/core';
22
import {bootstrap} from 'angular2/platform/browser';
33
import {Redify} from './directives';
4+
import {HighlightDirective} from './highlight.directive';
45

56
@Component({
67
selector: "app",
7-
directives:[Redify],
8+
directives:[Redify,HighlightDirective],
89
template: `
10+
redify:
911
<p redify >hello,lewis</p>
12+
myHighlight:
13+
<div>
14+
<input type="radio" name="colors" (click)="color='lightgreen'">Green
15+
<input type="radio" name="colors" (click)="color='yellow'">Yellow
16+
<input type="radio" name="colors" (click)="color='cyan'">Cyan
17+
</div>
18+
<p [myHighlight]="color">Highlight me!</p>
19+
<p [myHighlight]="color" [defaultColor]="'violet'">Highlight me too!</p>
1020
`
1121
})
1222
export class App {
13-
constructor() {
14-
15-
}
23+
constructor() {
24+
25+
}
1626
}
1727

1828
bootstrap(App, [])
19-
.catch(err => console.error(err));
29+
.catch(err => console.error(err));
2030

2131

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {Directive, ElementRef, Input} from 'angular2/core';
2+
3+
@Directive({
4+
selector: '[myHighlight]',
5+
host: {
6+
'(mouseenter)': 'onMouseEnter()',
7+
'(mouseleave)': 'onMouseLeave()'
8+
}
9+
})
10+
11+
export class HighlightDirective {
12+
/*
13+
@Input() myHighlight: string;
14+
*/
15+
@Input('myHighlight') highlightColor: string;
16+
17+
private _defaultColor = 'red';
18+
@Input() set defaultColor(colorName:string){
19+
this._defaultColor = colorName || this._defaultColor;
20+
}
21+
22+
constructor(private el: ElementRef) { }
23+
24+
onMouseEnter() { this._highlight(this.highlightColor || this._defaultColor); }
25+
onMouseLeave() { this._highlight(null); }
26+
27+
private _highlight(color:string) {
28+
this.el.nativeElement.style.backgroundColor = color;
29+
}
30+
}
31+
32+
/*
33+
Copyright 2016 Google Inc. All Rights Reserved.
34+
Use of this source code is governed by an MIT-style license that
35+
can be found in the LICENSE file at http://angular.io/license
36+
*/

0 commit comments

Comments
 (0)