2014-10-27 73 views
1

我已经使用ng-paste作为textarea,同时粘贴textarea中的链接,我正在调用一个自定义函数来存储该值。请参考以下代码angularjs ng-paste不更新模型值

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

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

但是在控制台中总是得到undefined的值。我如何获得我的对象值?

+0

编辑我的答案,希望它有帮助! – 2014-10-27 17:49:29

回答

4

由于您已经指定ng-model,因此将模型传递给函数并没有什么意义,所以当用户在textbox中键入内容时,它的值将会更新。如果您想跟踪更改,您可以为您的模型设置$watch或使用ng-change指定功能。

如果你想知道用户粘贴的是什么,那么这是另一回事。处理ng-paste可能会很棘手。要访问实际事件,最简单的方法是在angularjs之前包含jQuery,然后执行以下:

HTML模板

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

控制器

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

相关plunker这里http://plnkr.co/edit/ea5y5j


enter image description here

+0

什么是$ event.originalEvent responsability? – 2014-11-08 16:48:48

+0

http://stackoverflow.com/q/16674963/1061668 – 2014-11-08 18:16:03

0

只需使用$timeout即可在模型更新后调用粘贴回调。

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