Skip to content

Instantly share code, notes, and snippets.

@weihanchen
Created February 17, 2017 04:12
Show Gist options
  • Save weihanchen/b30cae8cfac1595efa0ff8ad01224a34 to your computer and use it in GitHub Desktop.
Save weihanchen/b30cae8cfac1595efa0ff8ad01224a34 to your computer and use it in GitHub Desktop.
Angular input select all on click
'use strict';
(function() {
angular.module('notes')
.controller('selectAllOnClickController', [selectAllOnClickController])
function selectAllOnClickController() {
var self = this;
self.text = "Click me to select all"
}
})();
'use strict';
(function() {
angular.module('note')
.directive('selectAllOnClick', selectAllOnClick)
function selectAllOnClick() {
return {
restrict: 'A',
controller: ['$element', selectAllOnClickController],
controllerAs: 'selectAllOnClickCtrl',
bindToController: true
}
function selectAllOnClickController($element) {
}
}
})()
'use strict';
(function() {
angular.module('note')
.directive('selectAllOnClick', selectAllOnClick)
function selectAllOnClick() {
return {
restrict: 'A',
controller: ['$element', selectAllOnClickController],
controllerAs: 'selectAllOnClickCtrl',
bindToController: true
}
function selectAllOnClickController($element) {
$element.on('click', function() {
var isInputSelected = $element[0].selectionEnd - $element[0].selectionStart != 0;
if (!isInputSelected) {
$element.select();
}
})
}
}
})()
<form class="form-inline">
<div class="form-group">
<input type="text" class="form-control" ng-model="selectAllOnClickCtrl.text" placeholder="Please input some text and test select all operation..." select-all-on-click />
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment