Angular 4 resetting form after update - keeping ngModel values (using ngForm) -
i feel should simple can't find (at least without lot of overhead coding) particular use case. have update profile page not modal. once user updates, want form reset original state , current values, in case user wants more edits.
i'm using ngform's reset()
(also tried resetform()
). resets pristine, etc., removes of values in inputs. on each of inputs i'm using [(ngmodel)]="userrecord.phone"
, etc. userrecord
still correct.
is there simple way keep or model values after reset?
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 error saving', err); this.displayerror = 'unknown error. please refresh page , try again.' this.isprocessing = false; }); }
if helps visualize, here original screen before submitting:
here after submitting, if ngform reset()
:
here if don't reset. hoping this, except pristine , submit button enabled again (although i'm noticing that's not happening if reset, anyways. :-( ).
before form submit clone form data tmp object:
tmpformdata: sometype = object.assign({}, currentdata);
then after reset()
can yourform.patchvalue(valuesfromtmpformdateobject)
Comments
Post a Comment