2

ng-paste在将链接粘贴到 textarea 时使用了 textarea,我正在调用自定义函数来存储该值。请参考以下代码

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

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

但在控制台中,我总是获得undefined价值。我怎样才能得到我的对象价值?

4

2 回答 2

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

于 2014-10-27T08:17:34.843 回答
0

只需$timeout在模型更新后调用您的粘贴回调。

$scope.getContent = function(a){
   $timeout(function () {console.log(a.content)});
}
于 2017-12-07T11:03:21.387 回答