Last active
June 10, 2020 00:09
-
-
Save chan-dev/7d700f5c3c06aaaaea7bf4c9cc18f2b1 to your computer and use it in GitHub Desktop.
Angular utility directive to be able to access elementRef in template
This file contains 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
// from: https://twitter.com/Waterplea/status/1270264621534982146 | |
@Directive({ | |
selector: '[elementRef]', | |
exportAs: 'elementRef' | |
}) | |
export class ElementDirective<T extends Element> extends ElementRef<T> { | |
constructor({ nativeElement }: ElementRef<T>) { | |
super(nativeElement); | |
} | |
} |
This file contains 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
<my-component #ref="elementRef" elementRef></my-component> | |
<button (click)="ref.nativeElement.focus()"> | |
Focus element | |
</button> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample:
https://stackblitz.com/edit/element-directive