and $scope.foo = function(val) { console.log(val); } I get 'undefined' on console." /> and $scope.foo = function(val) { console.log(val); } I get 'undefined' on console." /> and $scope.foo = function(val) { console.log(val); } I get 'undefined' on console."/>

ng-paste brings model before it's pasted

524 Views Asked by At

Let's say I have

<input type="text" ng-paste="foo(v)" ng-model="v">

and

$scope.foo = function(val) {
    console.log(val);
}

I get 'undefined' on console.

I think it's because when the moment ng-paste is called, model is still 'undefined' and then pasted value is coming after.

How can I use pasted strings using ng-paste?

1

There are 1 best solutions below

1
Chandru On BEST ANSWER

Try like this :

Angularjs :

template.html

<input type="text" ng-paste="foo($event)" ng-model="v">

controller.js

$scope.v = "";

$scope.foo = function(e) {
    console.log(e.originalEvent.clipboardData.getData('text/plain'));
}

Angular 2

template.html

<input type="text" (paste)="foo($event)" [(ngModel)]="v">

component.ts

v: any;

foo(e) {
    console.log(e.clipboardData.getData('text/plain'));
}