1

I have an angularjs directive which listens to on paste event of my textarea:

element.on('paste', function (event) { }); 

How do i get the pasted text? I tried:

event.clipboardData.getData('text/plain') 

... and:

event.originalEvent.clipboardData 

... but both didn't work. Any suggestions would be helpful.

0

1 Answer 1

6

It's really simple actually. You can just get the text with element.val() but you need to put it into setTimeout.

I did an example for you.

Working JSFiddle: http://jsfiddle.net/vxcjw45d/

HTML:

<body ng-app="myApp"> <div ng-controller="myController"> <textarea paste-example></textarea> <div>{{ pastedText }}</div> </div> </body> 

Javascript:

var myApp = angular.module('myApp', []); myApp.controller('myController', function($scope) { $scope.pastedText = ''; }); myApp.directive('pasteExample', function(){ var linkFn = function(scope, element, attrs) { element.on('paste', function() { setTimeout(function() { console.log(element.val()); scope.pastedText = element.val(); scope.$apply(); }, 5); }); }; return { restrict: 'A', link: linkFn }; }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.