1

i want to reset form (addUserForm) after clicking Save button . i made a modal with form to enter user data , i user this form for editing data and create new data and give the form flag Create and Update to distinguish between them , when i reload the page and click to open the modal for entering new user it's clear but when i click edit button firstly then new form i found the data which i edited.

onSaveUser function:

onUserSave(user: User){
    let newUser = {
        name: user.name,
        role: user.role,
        email: user.email,
        password: user.password,
    }
    if(this.flag == 'create'){
  this.addUserForm.reset();
        this.dataStorageService.storeUsers(newUser);
    }else{
        this.dataStorageService.editUser(this.userId, newUser);
    }
    this.modalRef.close();
    this.addUserForm.reset();
}

This is the html of the component which has the form :

<div [@routerTransition]>
    <app-page-header [heading]="' users '" [icon]="'fa-users'"></app-page-header>

    <a style="margin-right: 15px; cursor:pointer; padding: 0px;" role="button">
    <span (click)="open(content, 'create')"> add Users </span>
        <i class="fa fa-user-plus" style="color: red;"></i>
         <ng-template #content let-c="close" let-d="dismiss">
                    <!-- pop up -->
            <div class="modal-dialog modal-lg" dir="rtl">
             <div class="modal-header ">
              <h4 class="modal-title" style="color:red;">add new user</h4>
              <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
              <span aria-hidden="true">&times;</span>
             </button>
            </div>
                            <!-- pop up body -->
            <div class="modal-body">
            <form [formGroup]="addUserForm" (ngSubmit)="onSubmit(t)" #t="ngForm">
             <div class="row">
               <div class="form-group" class="col-lg-6">
                    <label >   username  </label>
                    <input class="form-control text-line" type="text" id="name" formControlName="name"  [(ngModel)]='name' required/>
                    <br />

                    <label class="padd">  password  </label>
                <input class="form-control text-line" type="password" id="password" formControlName="password" [(ngModel)]='password' required/>
                  </div>

                <div class="form-group" class="col-lg-6">
                <label>  Email </label>
                <input class="form-control  text-line" type="email" id="email" formControlName="email"  [(ngModel)]='email' required />
                <br />

                </div>
                <!-- modal footer -->
            <div class="modal-footer">
            <button type="submit" class="btn btn-success" (click)="onUserSave(addUserForm.value,t);" style="margin: 20px;" [disabled]="!t.valid" > حفظ </button>
            <button type="button" class="btn btn-danger" (click)="c('Close click')"> cancel</button>
                    </div>
                    </form>
                        <br />

                    </div>
                </div>
            </ng-template>
        </a>

            <hr>

            <table class="table table-hover table-condensed text-center">
                    <thead >
                            <tr class="text-center" style="background-color: #eef4f7; ">
                                    <th > # </th>

                                    <th > username                                      </th>

                                    <th >
                                            email
                                    </th>

                                    <th >
                                            edit
                                    </th>

                            </tr>
                    </thead>
                    <tbody>

                            <tr *ngFor="let user of users">
                                    <td>
                                            #
                                    </td>

                                    <td>
                                            {{user.name}}
                                    </td>

                                    <td>
                                            {{user.email}}
                                    </td>

                                    <td>
                  &nbsp;
                  <i class="fa fa-pencil-square-o fa-lg" style="cursor: pointer; color:#008000;" (click)="open(content, 'update', user)"></i>                       </td>
                            </tr>
                    </tbody>
            </table>

Aya Abdelaziz
  • 355
  • 3
  • 10
  • 23
  • 1
    How did you create the form (Template Driven or Model Driven)? Can you show the template? – Christian Vincenzo Traina Aug 26 '17 at 10:43
  • Possible duplicate of [Resetting a form in Angular 2 after submit](https://stackoverflow.com/questions/36655922/resetting-a-form-in-angular-2-after-submit) – angularrocks.com Aug 26 '17 at 11:00
  • @CristianTraina i edited and added the form – Aya Abdelaziz Aug 26 '17 at 11:01
  • You have an extra parameter and non accurate type for the other: (click)="onUserSave(addUserForm.value,t)" , and the signature for the function is onUserSave(user: User). – Vega Aug 26 '17 at 11:55
  • @Vega could u explain more ? what is the problem ? the problem in t parameter ? – Aya Abdelaziz Aug 26 '17 at 12:32
  • you call save from the template as : (click)="onUserSave(addUserForm.value,t)" . you have two parameters: addUserForm.value and t ! and look at the method in the class, how you wrote it – Vega Aug 26 '17 at 12:39

2 Answers2

1

I wasn't able to run your code as there are some missing parts, but you should be able to reset the form as :

..... (click)="onUserSave(addUserForm.value,t); addUserForm.reset()" .....
Vega
  • 27,856
  • 27
  • 95
  • 103
  • The same problem , when i click edit button then cancel then add new user i find the form has the previous values for editing part – Aya Abdelaziz Aug 26 '17 at 12:31
  • Because the function save didn't work, you have to fix it first. – Vega Aug 26 '17 at 12:32
  • i tested but that is not the problem , it's not working ,, Save button make me add another row in column with new data and it's working , i don't think the problem with onSaveUser function . – Aya Abdelaziz Aug 26 '17 at 12:51
  • i ask it doesn't reset after i click edit then begin a new form it doesn't reset .. Thank you for helping – Aya Abdelaziz Aug 26 '17 at 13:00
  • i edited my problem with complete HTML code .. Do u want any declaration ? – Aya Abdelaziz Aug 26 '17 at 13:15
  • As it needs no be fixed, it's hard to go through. One thing I could suggest, to put everything that is in ngOnInit in a separate method, lets say, userInit(), and call it from ngOnInit and also where you want to call reset() and instead of that – Vega Aug 26 '17 at 13:56
  • Thank you so much for helping , it solved by adding this.addUserForm.reset(); in ngOnInit() . – Aya Abdelaziz Aug 26 '17 at 14:06
1
<form [formGroup]="organisationSignUp" novalidate (submit)="OrganisationSignUp()">
</form>

in the component

OrganisationSignUp(){
// process form
....
....
// After done processing ...clear form like this

this.organisationSignUp.reset();
}

that is how to clear and reset form field after processing

Lekens
  • 1,823
  • 17
  • 31
  • This is the reactive forms strategy, it doesn't work with template driven strategy as per angular docs. Using those together is deprecated in angular 6 and will be removed in angular 7. – Sanket Berde May 28 '18 at 11:22