File tree Expand file tree Collapse file tree 2 files changed +51
-5
lines changed Expand file tree Collapse file tree 2 files changed +51
-5
lines changed Original file line number Diff line number Diff line change 11import { Component } from 'angular2/core' ;
22import { bootstrap } from 'angular2/platform/browser' ;
33import { 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} )
1222export class App {
13- constructor ( ) {
14-
15- }
23+ constructor ( ) {
24+
25+ }
1626}
1727
1828bootstrap ( App , [ ] )
19- . catch ( err => console . error ( err ) ) ;
29+ . catch ( err => console . error ( err ) ) ;
2030
2131
Original file line number Diff line number Diff line change 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+ */
You can’t perform that action at this time.
0 commit comments