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

Disable Cut, Copy and Paste function for textbox using AngularJs

本文关键字:复制 功能 AngularJs 文本      更新时间:2023-09-26

我想使用 angularJs 禁用文本区域中的复制粘贴。我尝试使用 ng-paste 来做到这一点,如下所示:

控制器:

  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" />

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

阻止粘贴第二次

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

尝试生成一个指令来侦听cutcopypaste事件,然后阻止默认事件操作。

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

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

<input stopccp ng-model="val" />

普伦克

您也可以使用 ng-copyng-cutng-paste 指令并直接取消事件。

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

普伦克

最简单的方法:

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

在这里工作

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

在控制器中

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

你可以这样做

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

这是演示普伦克