Last active
March 22, 2017 17:25
-
-
Save isaurssaurav/bb3ee7ce5619fcd671c3df9f1366cf4b to your computer and use it in GitHub Desktop.
angular2.HostBinding and HostListener example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Directive ,HostListener,HostBinding } from '@angular/core'; | |
| @Directive({ | |
| selector: '[highlight]', //element having hightlight [attribute] is selected or .class or #id or component | |
| }) | |
| export class HighlightDirective { | |
| @HostListener('mouseenter') mouseover(){ | |
| this.backgroundColor = 'green'; | |
| //can use renderer here to change style here | |
| } | |
| @HostListener('mouseleave') mouseleave(){ | |
| this.backgroundColor = 'white'; | |
| } | |
| @HostBinding('style.backgroundColor') get setColor(){ | |
| return this.backgroundColor; | |
| } //read only .. | |
| private backgroundColor = 'white'; | |
| constructor() { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment