Javascript 多个指令在Angularjs中不起作用
我有两个指令,一个用于避免剪切、复制和粘贴,另一个用于设置字符数限制。当我单独使用这些指令时,我没有收到任何错误,但当我试图在单个html输入框中使用这两个指令时,我收到以下错误: [$compile:multidir]多个指令[limitChar(模块:main)、stopcp(模块:main)]要求在以下位置上创建新的/隔离的作用域: 剪切复制指令:Javascript 多个指令在Angularjs中不起作用,javascript,angularjs,ionic-framework,Javascript,Angularjs,Ionic Framework,我有两个指令,一个用于避免剪切、复制和粘贴,另一个用于设置字符数限制。当我单独使用这些指令时,我没有收到任何错误,但当我试图在单个html输入框中使用这两个指令时,我收到以下错误: [$compile:multidir]多个指令[limitChar(模块:main)、stopcp(模块:main)]要求在以下位置上创建新的/隔离的作用域: 剪切复制指令: angular.module('main') .directive('stopccp', function (){ return {
angular.module('main')
.directive('stopccp', function (){
return {
restrict: 'EA',
scope: {},
link:function (scope, element) {
element.on('cut copy paste', function (event) {
event.preventDefault();
});
}
};
});
angular.module('main')
.directive('limitChar', function() {
return {
restrict: 'A',
scope: {
limit: '=limit',
ngModel: '=ngModel'
},
link: function(scope) {
scope.$watch('ngModel', function(newValue, oldValue) {
if (newValue) {
var length = newValue.toString().length;
if (length > scope.limit) {
scope.ngModel = oldValue;
}
}
});
}
};
})
文本长度指令:
angular.module('main')
.directive('stopccp', function (){
return {
restrict: 'EA',
scope: {},
link:function (scope, element) {
element.on('cut copy paste', function (event) {
event.preventDefault();
});
}
};
});
angular.module('main')
.directive('limitChar', function() {
return {
restrict: 'A',
scope: {
limit: '=limit',
ngModel: '=ngModel'
},
link: function(scope) {
scope.$watch('ngModel', function(newValue, oldValue) {
if (newValue) {
var length = newValue.toString().length;
if (length > scope.limit) {
scope.ngModel = oldValue;
}
}
});
}
};
})
Html:
最初,stopccp指令没有限制。在那之后,我限制使用E,我没有得到任何错误,但我能够剪切、复制和粘贴。那么,有人能告诉我该怎么做吗。您是如何做DI的,在DI中,您将指令添加到一个特定的视图中。@TGarrett我正在使用上述html输入的指令。错误明确指出,您在同一个元素上应用了两个指令,它们要求独立的作用域,这不适用于angularjs@PankajParkar但是我该怎么做呢?为什么不使用maxlength HTML属性作为输入字段呢?