I have successfully incorporated a solution to sanitize copied text using a custom filter that takes into account the current input selection, including support for IE11.
JS:
vm.pasteInput = pasteInput;
function pasteInput(ev){
var pastedData ='';
var domElement = ev.currentTarget;
if(typeof ev.originalEvent.clipboardData !== "undefined"){
pastedData = ev.originalEvent.clipboardData.getData('text/plain');
} else if(window.clipboardData){
pastedData = window.clipboardData.getData('Text');
ev.returnValue = false;
}
pastedData = $filter('inputtext')(pastedData);
// insert at correct position within selection
if (document.selection) { // IE11
domElement.focus();
var sel = document.selection.createRange();
sel.text = pastedData;
domElement.focus();
} else if (domElement.selectionStart || domElement.selectionStart === 0){
var startPos = domElement.selectionStart;
var endPos = domElement.selectionEnd;
domElement.value = domElement.value.substring(0, startPos) + pastedData + domElement.value.substring(endPos, domElement.value.length);
domElement.focus();
domElement.selectionStart = startPos + pastedData.length;
domElement.selectionEnd = startPos + pastedData.length;
} else {
domElement.value += pastedData;
domElement.focus();
}
ev.stopPropagation();
ev.preventDefault();
// update model
vm.input.value = domElement.value;
}
HTML:
<input type="text" ng-model="vm.input.value" ng-paste="vm.pasteInput($event)" />