Skip to content

Instantly share code, notes, and snippets.

@chan-dev
Last active June 10, 2020 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chan-dev/7d700f5c3c06aaaaea7bf4c9cc18f2b1 to your computer and use it in GitHub Desktop.
Save chan-dev/7d700f5c3c06aaaaea7bf4c9cc18f2b1 to your computer and use it in GitHub Desktop.
Angular utility directive to be able to access elementRef in template
// 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);
}
}
<my-component #ref="elementRef" elementRef></my-component>
<button (click)="ref.nativeElement.focus()">
Focus element
</button>
@chan-dev
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment