Angular 2 reset form to initial data without having to set each formcontrol individually -


i have formgroup open in edit mode , initialize proper data. save initial data in editform.

after doing changes want reset changes initial values, like:

this.formgroup.reset(this.stafftoedit); 

but put blank in every field. workaround found

this.formgroup.reset({field1 : this.editform.field1}); ... 

the thing need more generic resets fields initial value without having go through each formcontrol.

is possible do?

what keep reference of initial values , use patchvalue() call markaspristine() set form values were, , marking entire form pristine.

const initialformvalues {   firstname: 'joe',   surname: 'doe' };  ...  reset() {   this.formgroup.patchvalue(initialformvalues);   this.formgroup.markaspristine(); } 

or i'm guessing in case:

reset() {   this.formgroup.patchvalue(this.editform.value);   this.formgroup.markaspristine(); } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -