我尝试使用粘贴的字符串做一些事情,但是当我尝试删除内容时我似乎无法做到这一点,因为粘贴的内容不受限制到输入的模型。
如何清除粘贴内容的输入?
我尝试将内容绑定到模型然后删除模型,但仍然会将实际粘贴的内容留在事件对象中,因此它不是解决方案。
还尝试使用input.value = ''
直接清除输入,但没有运气。
标记:
<input #input [(ngModel)]="newTag[labelKey]" (paste)="onPaste($event)">
功能:
onPaste(e: any) {
let content = e.clipboardData.getData('text/plain');
// Do stuff
// Then clear pasted content from the input
}
答案 0 :(得分:12)
清除绑定模型的变量:
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
由于某种原因,当同步调用onPaste(e: any) {
let content = e.clipboardData.getData('text/plain');
// Do stuff
setTimeout(() => {
this.newTag = "";
}, 0);
}
时,文字会保留在输入中,如果您将其设置为this.newTag = ""
,则输入中的文字为this.newTag = "foo"
。
工作示例