Angular 角度5表单复制输入值
我有一个简单的表格:Angular 角度5表单复制输入值,angular,angular-reactive-forms,Angular,Angular Reactive Forms,我有一个简单的表格: this.searchForm = this.formBuilder.group({ query: [ null, [Validators.required] ] }); 当用户将某些内容粘贴到输入中时,我使用regex重新格式化值并更新表单 onPaste(event) { const formattedQuery = event.clipboardData.getData('text/plain') .split(/,?[\r\n\t]+\s?
this.searchForm = this.formBuilder.group({
query: [ null, [Validators.required] ]
});
当用户将某些内容粘贴到输入中时,我使用regex重新格式化值并更新表单
onPaste(event) {
const formattedQuery = event.clipboardData.getData('text/plain')
.split(/,?[\r\n\t]+\s?/)
.join(', ')
.replace(/,\s?$/g, '');
this.searchForm.get('query').setValue(formattedQuery);
// this.ref.detectChanges();
}
但是当我粘贴一些东西时,例如
325435956
325435956
它复制了它,因此我看到了325435956325435956325435956325435956325435956
,但我希望看到325435956325435956
。我的错误在哪里?如何纠正
您可以在此处找到的工作示例即使您处理粘贴函数,默认行为仍保持不变 因此,它首先处理该方法并打印出期望值。然后按原样粘贴值 您应该防止默认行为
onPaste(event) {
event.preventDefault();
//rest of the method as it is right now
}
固定示例:在
onPaste
方法中使用此行this.searchForm.setValue({value:'})
,然后重试一次。这将在粘贴时清除表单值,我猜该值将保留在对象中