Created
June 30, 2022 04:13
-
-
Save NathanWalker/c99a1d8ed3ded6c252dc57309cfb7ac9 to your computer and use it in GitHub Desktop.
NativeScript for Angular Directive using @HostListener sample
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, ElementRef, HostListener, inject } from "@angular/core"; | |
@Directive({ | |
selector: "[appHighlight]", | |
}) | |
export class HighlightDirective { | |
currentColor = ""; | |
private el = inject(ElementRef); | |
@HostListener("tap") onTap() { | |
if (this.currentColor) { | |
this.currentColor = ""; | |
} else { | |
this.currentColor = "yellow"; | |
} | |
this.highlight(this.currentColor); | |
} | |
private highlight(color: string) { | |
this.el.nativeElement.style.backgroundColor = color; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment