使用AngularJs禁用文本框的剪切,复制和粘贴功能

Mar*_*thu 13 javascript jquery angularjs

我想使用angularJs在textarea中禁用复制粘贴.我试着用ng-paste这样做,像这样:

控制器:

  angular.module('inputExample', [])
  .controller('ExampleController', ['$scope', function($scope) {

  $scope.val = '1';
  $scope.past = function() {

    console.log("d");
    $scope.val ="  ";

  }
}]);
Run Code Online (Sandbox Code Playgroud)

HTML:

<input ng-paste="past()" ng-model="val" ng-pattern="/^\d+$/" name="anim" class="my-input" />
Run Code Online (Sandbox Code Playgroud)

输入框具有旧数据(初始粘贴数据).

阻塞粘贴第二次工作,也就是说,如果我将数据粘贴到输入框中,数据将会出现,但是在第二次粘贴时,数据将不会粘贴,旧的数据值不会被删除.

quw*_*quw 30

尝试使侦听FOT的一个指令cut,copypaste事件,然后防止违约事件采取行动.

app.directive('stopccp', function(){
    return {
        scope: {},
        link:function(scope,element){
            element.on('cut copy paste', function (event) {
              event.preventDefault();
            });
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

通过将属性添加到输入框来使用.

<input stopccp ng-model="val" />
Run Code Online (Sandbox Code Playgroud)

Plunker

你也可以使用ng-copy,ng-cutng-paste指示和直接取消事件.

<input ng-cut="$event.preventDefault()" ng-copy="$event.preventDefault()" ng-paste="$event.preventDefault()" ng-model="val" />
Run Code Online (Sandbox Code Playgroud)

Plunker


ism*_*tro 19

最简单的方法:

<input ng-paste="$event.preventDefault();" placeholder='You cannot past here'>
Run Code Online (Sandbox Code Playgroud)

在这里工作


K.T*_*ess 0

你可以这样做

app.controller('MainCtrl', function($scope, $timeout) {....
.......
$scope.past = function() {
   $timeout(function() {
      $scope.val = " ";
   }, 0);
}...
Run Code Online (Sandbox Code Playgroud)

这是演示 Plunker