Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Detection Quirk with NgForm.resetForm() #19835

Closed
jongunter opened this issue Oct 20, 2017 · 5 comments
Closed

Change Detection Quirk with NgForm.resetForm() #19835

jongunter opened this issue Oct 20, 2017 · 5 comments

Comments

@jongunter
Copy link

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Right after calling resetForm() on an instance of NgForm (to clear out validation and mark everything as pristine) I then change a value that one of form controls is bound to (using template-driven forms and ngModel). The control does not update with the new value, but instead gets stuck with the null value that resetForm() sets on it. This only happens if the control is untouched.

Expected behavior

Change detection should pick up the change I make to the ngModel-bound variable after calling resetForm().

Minimal reproduction of the problem with instructions

Using the example below

  1. Fill out the name field
  2. Choose "Yes"
  3. Press "Submit".
  4. The form should reset and the dropdown should revert to "No", as expected.
  5. Fill out the name field again
  6. Choose "No"
  7. Press "Submit"
  8. The form should reset, but the dropdown is reverted to an empty value, rather than "No"
import {Component, ViewChild} from '@angular/core';
import {cloneDeep} from 'lodash';
import {NgForm} from '@angular/forms';

class User {
  isHappy = false;
  name = '';
}

@Component({
  selector: 'app-root',
  template: `
    <form #myForm="ngForm" (ngSubmit)="submit()">
      <div class="form-group">
        Name
        <input [(ngModel)]="user.name" name="name">
      </div>

      <div class="form-group">
        Are you happy?
        <select [(ngModel)]="user.isHappy" name="isHappy">
          <option [ngValue]="true">Yes</option>
          <option [ngValue]="false">No</option>
        </select>
      </div>

      <button type="submit">
        Submit
      </button>

      <pre>
    {{ user | json }}
  </pre>

    </form>

  `,
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  user = new User();

  @ViewChild('myForm') myForm: NgForm;

  submit(){
    console.log('submitted', cloneDeep(this.user));
    this.myForm.resetForm();
    this.user = new User();
  }

}

What is the motivation / use case for changing the behavior?

The behavior is confusing. Even if we don't change Angular's response, it should probably be documented somewhere.

Environment


Angular version: 4.4.5

Browser:
- [ x] Chrome (desktop) Version 61.0.3163.100 (Official Build) (64-bit)
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
@jongunter
Copy link
Author

I should mention that if I call

this.myForm.resetForm({ isHappy: false });

This issue does not appear. The problem is that

this.user = new User();

should always set the isHappy control to false, right?

Also, I said

This only happens if the control is untouched.

I was wrong. Actually, it seems to happen if the control's value is false when resetForm() is called, regardless of if the control it touched/pristine or not. If the control's value is true, when resetForm() is called the control displays correctly after resetForm().

@jasonaden
Copy link
Contributor

Sounds like an issue that needs to be fixed. Can you please create a running example of your reproduction? We suggest using StackBlitz as you will get a full VSCode environment.

@kara
Copy link
Contributor

kara commented Nov 30, 2017

This is actually working as expected. If you want to reset the form to a certain value, you must pass that value into the reset function.

this.user = new User();
this.form.resetForm(this.user);

@kara kara closed this as completed Nov 30, 2017
@aec4444
Copy link

aec4444 commented Jan 10, 2018

This isn't working as expected:

this.user = new User();
this.form.resetForm(this.user);

Is clearing the fields, but it isn't doing this:

  1. Resetting the controls to the this.user.
  2. Populating dropdowns to the value in transpiler: show line numbers that have errors #1 above
  3. Removing ng-submitted from the form.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants