禁用剪切,复制和粘贴功能,使用文本框AngularJs [英] Disable Cut, Copy and Paste function for textbox using AngularJs
问题描述
我想用angularJs一个textarea禁止复制粘贴。我试着用NG粘贴这样做,就像这样:
I want to disable copy paste in a textarea using angularJs. I tried to do so with ng-paste, like so:
控制器:
angular.module('inputExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.val = '1';
$scope.past = function() {
console.log("d");
$scope.val =" ";
}
}]);
HTML
<input ng-paste="past()" ng-model="val" ng-pattern="/^\d+$/" name="anim" class="my-input" />
输入框有旧的数据(最初粘贴数据)。
Input box has old data ( the initial paste data).
禁止粘贴工作第二次左右,即如果我粘贴到数据输入框,该数据将是present,但在第二粘贴数据不会糊,的但旧的数据值不会被删除。
Blocking paste works the second time around, that is if I paste the data into input box, the data will be present, but on a second paste the data won't paste, but the old data value is not removed.
推荐答案
试着做侦听FOT的削减
,复制$指令C $ C>和
粘贴
事件,然后prevent默认的事件动作。
Try making a directive that listens fot the cut
, copy
, and paste
events and then prevent the default event action.
app.directive('stopccp', function(){
return {
scope: {},
link:function(scope,element){
element.on('cut copy paste', function (event) {
event.preventDefault();
});
}
};
});
通过添加属性添加到输入框中使用。
Use by adding the attribute to the input box.
<input stopccp ng-model="val" />
<大骨节病> Plunker 骨节病>
您也可以使用 NG-复制
, NG-削减
和 NG-粘贴
指令,并直接取消事件。
You could also use the ng-copy
, ng-cut
and ng-paste
directives and directly cancel the event.
<input ng-cut="$event.preventDefault()" ng-copy="$event.preventDefault()" ng-paste="$event.preventDefault()" ng-model="val" />
<大骨节病> Plunker 骨节病>
这篇关于禁用剪切,复制和粘贴功能,使用文本框AngularJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!