Skip to content

Instantly share code, notes, and snippets.

@tperrelli
Created April 12, 2018 14:16
Show Gist options
  • Save tperrelli/fd17c2fe95c8b17b460b15e80687f9f6 to your computer and use it in GitHub Desktop.
Save tperrelli/fd17c2fe95c8b17b460b15e80687f9f6 to your computer and use it in GitHub Desktop.
Select All Directive
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: 'ion-searchbar[select-all],ion-input[select-all]'
})
export class SelectAll {
constructor(private el: ElementRef) {
}
@HostListener('ionFocus')
selectAll() {
// access to the native input element
let nativeEl: HTMLInputElement = this.el.nativeElement.querySelector('input');
if (nativeEl) {
if (nativeEl.setSelectionRange) {
// select the text from start to end
return nativeEl.setSelectionRange(0, nativeEl.value.length);
}
nativeEl.select();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment