Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo changes to model state when angular form is canceled

In an Angular 4 application, I have a template driven form with controls bound to values in my component. When the form input changes the object bound to the input changes immediately (two-way binding). When the cancel button is clicked, I want to undo the change to the bound object. This allows the user to change values then change their mind and cancel their changes.

https://plnkr.co/edit/RnnPwtHZY0qTN1H6er0z?p=preview

The plunker above has such a form with a bound field to read the hero.name

  <h2>{{hero.name}} details!</h2>

An input bound to a hero object.

  <form #myForm="ngForm" (ngSubmit)="save(myForm)">
    <div class="form-group">
      <label>name: </label>
      <input name="heroName" [(ngModel)]="hero.name" placeholder="name" />
    </div>
    <button (click)="cancel(myForm)">Cancel</button>
    <button type="submit">Save</button>
  </form>

The cancel button calls the ngForm's resetForm() method.

  cancel(myForm){
    myForm.resetForm();
  }

Repro steps

  1. Change the hero name; Observe the h2 changes immediately proving that the bound object changed as well
  2. Click cancel; Observe the name is cleared and the h2 changes because the hero.name is now null

I expected the cancel button to change the hero.name back to the original value. Is this how resetForm() is supposed to work? Is there a different way?

like image 334
Rob Murdoch Avatar asked Oct 11 '17 19:10

Rob Murdoch


People also ask

How to reset form data in angular?

In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.

How to reset a form control in angular?

import { FormsModule } from '@angular/forms'; In Reactive forms, we need to import FormGroup from '@angular/forms' . After importing the above-mentioned modules in the respective approach, angular forms module provides an inbuilt method called reset(). We can use the method and we can reset the form.

How to reset a template driven form in angular?

To reset template-driven form, NgForm provides resetForm() method that is called as following. To call the above function, create a button in UI. If we want to reset form with some default values, then assign the default values with form control name of the form.

How to reset form in angular 10?

you can use formGroup reset method like this (click)="noteForm. reset()" and this method will marks all descendants are marked pristine and untouched, and the value of all descendants to null.


2 Answers

If you make a reset() method, where you set the default values, then you can call it whenever it's needed, as in ngOninit and reset button click:

  ngOnInit(){
    this.reset();
  }
  reset(){
    this.hero = new Hero(1,'Plunker');
  }

  cancel(myForm){
    this.reset();
  }

DEMO

You can reunite cancel() and reset() by refactoring, but you may want keep it as is in case you add something else in cancel.

like image 120
Vega Avatar answered Oct 17 '22 10:10

Vega


From the angular documentation, I guess the solution is to use a reactive form instead of a template driven form. https://angular.io/guide/reactive-forms reads (emphasis :

In keeping with the reactive paradigm, the component preserves the immutability of the data model, treating it as a pure source of original values. Rather than update the data model directly, the component extracts user changes and forwards them to an external component or service, which does something with them (such as saving them) and returns a new data model to the component that reflects the updated model state.

like image 2
Rob Murdoch Avatar answered Oct 17 '22 12:10

Rob Murdoch