我覺得這應該很簡單,但我找不到任何東西(至少沒有大量的開銷編碼),我的特殊用例。基本上我有一個不是模式的更新配置文件頁面。一旦用戶更新,我希望表單重新恢復到原始狀態並使用當前值,以防用戶想要進行更多編輯。更新後的角度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,但我可以從數據庫刷新我想。 –