Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Paste" event in Angular [ngPaste]

How to execute a function on "Paste" event in input in Angular 1.1.5? I know there is a ng-change directive for input. But it fires each time the input changes, I need only once on initial paste.

Use-case: I have a URL input. I want to execute a function after user pastes the URL. User also can manually enter the URL and execute the function by pressing Enter.

--

Update: Since Angular 1.2.0, ngPaste is a native directive.

like image 243
Websirnik Avatar asked Aug 26 '13 23:08

Websirnik


2 Answers

Since Angular 1.2.0 there is an ngPaste directive. Use the following way:

<input type='text' ng-paste='handlePaste($event)'>

To pass the value straightaway, use:

<input type='text' ng-paste='handlePaste($event.clipboardData.getData('text/plain'))'>
like image 151
Websirnik Avatar answered Sep 21 '22 06:09

Websirnik


In function you should use originalEvent

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

Function:

$scope.paste = function (e) {
    console.log(e.originalEvent.clipboardData.getData('text/plain'));
}
like image 24
Levsha Avatar answered Sep 20 '22 06:09

Levsha