2

I have used ng-paste for textarea while pasting the link in textarea, i am calling a custom function to store that value. Please refer following code

<textarea rows="1" ng-model="myObj.content"
              ng-paste="getContent(myObj)">
 </textarea>

$scope.getContent = function(a){
    console.log(a.content);
}

But in console always I am getting undefined value. How can I get my object value?

Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
Eswara Reddy
  • 1,617
  • 1
  • 18
  • 28

2 Answers2

5

Passing model to function does not really make sense since you have already specified ng-model, so it's value will be updated as user types something into the textbox. If you want to track changes you can setup a $watch for your model or specify a function using ng-change.

If you want to know what user pasted, then that's another story. Handling ng-paste can be tricky. To access the actual event, easiest is to include jQuery before angularjs and then do e.g. following:

HTML template

<textarea rows="3"
          placeholder="copy/paste here..."
          ng-init="content = null"
          ng-model="content" 
          ng-paste="paste($event.originalEvent)">
</textarea>

Controller

$scope.paste = function (event) {
  var item = event.clipboardData.items[0];
  item.getAsString(function (data) {
    console.log(data);
  });
};

Related plunker here http://plnkr.co/edit/ea5y5j


enter image description here

Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
0

Simply use $timeout to call your paste callback after the model has been updated.

$scope.getContent = function(a){
   $timeout(function () {console.log(a.content)});
}
Colm Seale
  • 179
  • 6