lundi 30 mars 2015

Creating a select all text button in angularJS

I've searched around for answers, but haven't found anything directly specific to this -- inside a modal, I have a disabled/read-only textarea, and a button to select all the text inside it, which the user can then copy (if anyone knows how to make a copy function along with the select button, that would be neat). However, when the button is clicked, while it looks like it's selecting all the textarea text, it's in fact only selecting the textarea element -- when you go to copy and paste text, nothing is available in the clipboard. Here's the relevant code:



$scope.selectAll = function() {
var selection = $window.getSelection();
var range = document.createRange();
var text = angular.element('textarea');
range.selectNodeContents(text[0]);
selection.removeAllRanges();
selection.addRange(range);
};


In the HTML:



<div class="modal-footer">
<button class="btn btn-default" data-ng-click="selectAll()">Select All</button>
<button class="btn btn-warning" data-ng-click="close()">Close</button>
</div>

Aucun commentaire:

Enregistrer un commentaire