The way to do this in Angular is to create a custom directive which does the autoselect for you.
module.directive('selectOnClick', ['$window', function ($window) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.on('click', function () {
if (!$window.getSelection().toString()) {
// Required for mobile Safari
this.setSelectionRange(0, this.value.length)
}
});
}
};
}]);
Apply the directive like this:
<input type="text" value="test" select-on-click />
View demo
Update1: Removed jQuery dependency.
Update2: Restrict as attribute.
Update3: Works in mobile Safari. Allows selecting part of the text (requires IE>8).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…