How to clear form after submit in Angular 2?

后端 未结 15 1397
遇见更好的自我
遇见更好的自我 2020-12-02 16:10

I have some simple angular 2 component with template. How to clear form and all fields after submit?. I can\'t reload page. After set data with date.setValue(\'\')

相关标签:
15条回答
  • 2020-12-02 16:44

    To reset your form after submitting, you can just simply invoke this.form.reset(). By calling reset() it will:

    1. Mark the control and child controls as pristine.
    2. Mark the control and child controls as untouched.
    3. Set the value of control and child controls to custom value or null.
    4. Update value/validity/errors of affected parties.

    Please find this pull request for a detailed answer. FYI, this PR has already been merged to 2.0.0.

    Hopefully this can be helpful and let me know if you have any other questions in regards to Angular2 Forms.

    0 讨论(0)
  • 2020-12-02 16:44

    I found another solution. It's a bit hacky but its widely available in angular2 world.

    Since *ngIf directive removes the form and recreates it, one can simply add an *ngIf to the form and bind it to some sort of formSuccessfullySentvariable. => This will recreate the form and therefore reset the input control statuses.

    Of course, you have to clear the model variables also. I found it convenient to have a specific model class for my form fields. This way i can reset all fields as simple as creating a new instance of this model class. :)

    0 讨论(0)
  • 2020-12-02 16:44

    Hm, now (23 Jan 2017 with angular 2.4.3) I made it work like this:

    newHero() {
        return this.model = new Hero(42, 'APPLIED VALUE', '');
    }
    <button type="button" class="btn btn-default" (click)="heroForm.resetForm(newHero())">New Hero</button>
    
    0 讨论(0)
  • 2020-12-02 16:44

    To reset the complete form upon submission, you can use reset() in Angular. The below example is implemented in Angular 8. Below is a Subscribe form where we are taking email as an input.

    <form class="form" id="subscribe-form" data-response-message-animation="slide-in-left" #subscribeForm="ngForm"
    (ngSubmit)="subscribe(subscribeForm.value); subscribeForm.reset()">
    <div class="input-group">
       <input type="email" name="email" id="sub_email" class="form-control rounded-circle-left"
          placeholder="Enter your email" required ngModel #email="ngModel" email>
       <div class="input-group-append">
          <button class="btn btn-rounded btn-dark" type="submit" id="register"
             [disabled]="!subscribeForm.valid">Register</button>
       </div>
    </div>
    </form>
    

    Refer official doc: https://angular.io/guide/forms#show-and-hide-validation-error-messages.

    0 讨论(0)
  • 2020-12-02 16:45

    To angular version 4, you can use this:

    this.heroForm.reset();
    

    But, you could need a initial value like:

    ngOnChanges() {
     this.heroForm.reset({
      name: this.hero.name, //Or '' to empty initial value. 
      address: this.hero.addresses[0] || new Address()
     });
    }
    

    It is important to resolve null problem in your object reference.

    reference link, Search for "reset the form flags".

    0 讨论(0)
  • 2020-12-02 16:50
    import {Component} from '@angular/core';
    import {NgForm} from '@angular/forms';
    @Component({
        selector: 'example-app',
        template: '<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
            <input name="first" ngModel required #first="ngModel">
            <input name="last" ngModel>
            <button>Submit</button>
        </form>
        <p>First name value: {{ first.value }}</p>
        <p>First name valid: {{ first.valid }}</p>
        <p>Form value: {{ f.value | json }}</p>
        <p>Form valid: {{ f.valid }}</p>',
    })
    export class SimpleFormComp {
        onSubmit(f: NgForm) {
    
            // some stuff
    
            f.resetForm();
        }
    }
    
    0 讨论(0)
提交回复
热议问题