angular type = [number]指令来阻止粘贴
问题描述:
我正在寻找一种方法来阻塞粘贴到数字指令中。 我虽然关于阻止所有的粘贴事件,我以前在jQuery中做过,但我没有做到与角度相同。
这是jQuery代码,我知道我需要扩展input[number]指令,但我找不到任何扩展此指令的示例。angular type = [number]指令来阻止粘贴
$("input[type='number']").on('paste', function (e) {
e.preventDefault();
var value = prompt('set content here');
this.value = (value)?value.replace(/[^\d.-]/g, ''):0;
})
我希望的码骨架或延伸的输入[数字]指令,所以我可以为此porpuse改变的演示。
答
因此,而不是延伸指令,我创造了在它的上面又一个... 希望此举能帮助任何人谁需要这个解决方案
myApp.directive('input', function() {
return {
scope: {},
link: function ($scope, $element, $attrs) {
if ($attrs.type == 'number') {
$element.on('paste', function (e) {
e.preventDefault();
var value = prompt('paste content here');
this.value = (value)?value.replace(/[^\d.-]/g, ''):0;
});
}
}
};
});