Created
April 12, 2018 14:16
-
-
Save tperrelli/fd17c2fe95c8b17b460b15e80687f9f6 to your computer and use it in GitHub Desktop.
Select All Directive
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
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