4

I'm having problems when i try to get the pasted data using ng-paste directive.

<input ng-model="evidence.url" ng-paste="getEvidenceInfos(evidence, $event.clipboardData.getData('text/plain'))">

When the jquery is loaded after angular, it works fine.
How to do when jquery is loaded before angular ?

<input ng-model="evidence.url" ng-paste="getEvidenceInfos(evidence, $event)">

$scope.getEvidenceInfos = function(evidence, event) {
  $(event.currentTarget).val() // doesn't work
  $(event.target).val() // doesn't work
}
4
  • Well, it looks like you are passing evidence as the first parameter to getEvidenceInfos() but your definition accepts event as the first parameter. Try adding a second parameter to your function and looking at that.
    – Joel Jeske
    May 18, 2014 at 22:45
  • Sorry, this example is wrong. The function receives two parameters! May 18, 2014 at 23:21
  • Ok gotcha. Can you be more specific about what you are wanting and what it is doing in actuality? A plunker or fiddle would greatly help as well.
    – Joel Jeske
    May 18, 2014 at 23:36
  • It's simple, i want to get the user pasted data on input field and get the value on getEvidenceInfos function May 18, 2014 at 23:40

2 Answers 2

9

When jQuery is loaded before angular, it is used within angular instead of jQuery lite which is built into angular. If full jQuery is used, you will receive a jQuery event. The raw event can be found in the property originalEvent.

$scope.getEvidenceInfos = function(evidence, event) {
    var pastedData = event.originalEvent.clipboardData;
}
1
3

Wanted to share my solution to support browsers without clipboard API. This should work on any browser that angular supports.

HTML:

<input type="text" ng-paste="copyPasted($event)">

JS:

$scope.copyPasted = function ($event){
    if(typeof $event.originalEvent.clipboardData !== "undefined"){
        $scope.handlePastedData($event.originalEvent.clipboardData.getData('text/plain'));
    } else { // To support browsers without clipboard API (IE and older browsers)
        $timeout(function(){
            $scope.handlePastedData(angular.element($event.currentTarget).val());
        });
    }
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.