我觉得这应该很简单,但我找不到任何东西(至少没有大量的开销编码),我的特殊用例。基本上我有一个不是模式的更新配置文件页面。一旦用户更新,我希望表单重新恢复到原始状态并使用当前值,以防用户想要进行更多编辑。更新后的角度4重置表单 - 保持ngModel值(使用ngForm)
我使用ngForm的reset()
(也试过resetForm()
)。这将它重置为原始等,但它也会删除输入中的所有值。在我使用的每个输入上使用[(ngModel)]="userRecord.phone"
等。我的userRecord
仍然正确。
是否有简单的方法来保留或取回重置后的模型值?
onSubmit(event, profileForm: NgForm) {
event.preventDefault();
const myTempVar = this.userRecord;
this.userRecord.put()
.catch((e) => {
this.isProcessing = false;
return Observable.of(false);
})
.subscribe((response) => {
this.displayConfirmation = 'Profile has been updated successfully.'
this.isProcessing = false;
this.userRecord = myTempVar;
}, (err) => {
console.log('There was an error saving', err);
this.displayError = 'Unknown error. Please refresh this page and try again.'
this.isProcessing = false;
});
}
这如果我不做重置。我希望它是这样的,除了还质朴,并得到提交按钮再次启用(虽然现在我注意到,如果我做了复位是没有发生,反正:-()。
当用户提交from时,是否将新值应用于'userRecord'变量? –
是的,完全是约翰 - 不希望数据发生变化。 –
Praveen,提交后我不会将任何新值应用于userRecord,但我可以从数据库刷新我想。 –