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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27783144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:39:54  来源:igfitidea点击:

Disable Cut, Copy and Paste function for textbox using AngularJs

javascriptjqueryangularjs

提问by Marimuthu

I want to disable copy paste in a textarea using angularJs. I tried to do so with ng-paste, like so:

我想使用 angularJs 在 textarea 中禁用复制粘贴。我尝试使用 ng-paste 这样做,如下所示:

Controller:

控制器:

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

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

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

  }
}]);

HTML:

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).

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

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, butthe old data value is not removed.

阻止粘贴第二次起作用,也就是说,如果我将数据粘贴到输入框中,数据将存在,但在第二次粘贴时,数据不会粘贴,不会删除旧数据值。

回答by quw

Try making a directive that listens fot the cut, copy, and pasteevents and then prevent the default event action.

试着做一个指令,监听FOT的cutcopypaste事件,然后防止违约事件采取行动。

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

Plunker

You could also use the ng-copy, ng-cutand ng-pastedirectives and directly cancel the event.

您还可以使用ng-copy,ng-cutng-paste指令并直接取消事件。

<input ng-cut="$event.preventDefault()" ng-copy="$event.preventDefault()" ng-paste="$event.preventDefault()" ng-model="val" />

Plunker

Plunker

回答by ismaestro

The simplest way:

最简单的方法:

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

Working here

在这里工作

回答by Mehmet Otkun

Try this;

 <input type="text" ng-paste="paste($event)" ng-model="name"/>

In Controller

在控制器中

 app.controller('MainCtrl', function($scope) {
    $scope.name = 'World';
    $scope.paste = function(e){
       e.preventDefault();
       return false
    }
 });

回答by Kalhan.Toress

you can do like this

你可以这样做

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

here is the Demo Plunker

这是演示Plunker