2017-07-19 63 views
6

我使用的是angular 2.4.0,我想在表單提交後清除輸入字段。 試過SendRequest.reset()在模板驅動的表單提交表單後重置表單Angular2

component.ts

getRevision(SendRequest: NgForm){ 
    this.submitted = true; 
    let headers = new Headers({ 
     'Content-Type': 'application/x-www-form-urlencoded' 
    }); 
    let params = { 
     "Firstname":this.name, 
     "Lastname":"", 
     "Email":this.email, 
     "Phonecode":"91", 
     "Phone":this.phone, 
     "Message":this.message,  
     } 
    return this.http.post(this.url,params) 
    .map(res => res.json()) 
    .catch(this.errorHandler) 
    .subscribe(
      data => { 
       this.revisions = data.resultData; 
       this.resultCode = data.resultCode; 
       this.responeseMsg = data.message.MessageText;     
      }, 
      err => { 
       console.log(err); 
      }, 
      () =>{ 
      SendRequest.resetForm(); 
      SendRequest.form.reset(); 
      } 
     ) 

    } 

component.html

<form #SendRequest="ngForm" [formGroup]="SendRequest" > 
    <div class="col-md-6"> 
    <div class="form-group"> 
    <input type="text" name="name" [(ngModel)]="name" class="form-control" placeholder="Your Name" required #fullname="ngModel"> 
    <div *ngIf="fullname.errors && (fullname.dirty || fullname.touched)" 
    class="contact-validate"> 
    <div [hidden]="!fullname.errors.required"> 
     Name is required 
    </div>          
    </div> 
</div> 
<div class="form-group"> 
    <input type="email" name="email" class="form-control" [(ngModel)]="email" required #emailid="ngModel" placeholder="Your Email" pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$"> 
    <div *ngIf="emailid.errors && (emailid.dirty || emailid.touched)" 
    class="contact-validate"> 
    <div [hidden]="!emailid.errors.required"> 
    Email is required 
    </div> 
    <div [hidden]="!emailid.errors.pattern"> 
    Please enter valid email format 
    </div>          
</div> 
</div> 
<div class="form-group"> 
<input type="text" name="phone" class="form-control" [(ngModel)]="phone" placeholder="Your Phone" required pattern="[0-9]+" #phoneno="ngModel"> 
<div *ngIf="phoneno.errors && (phoneno.dirty || phoneno.touched)" 
class="contact-validate"> 
<div [hidden]="!phoneno.errors.required"> 
    Phone is required 
</div>  
<div [hidden]="!phoneno.errors.pattern"> 
    Phone must be number 
</div>               
</div> 
</div> 
</div> 
<div class="col-md-6"> 
    <div class="form-group"> 
    <textarea class="form-control" name="message" [(ngModel)]="message" #msg="ngModel" required placeholder="Your Message" rows="7"></textarea> 
    <div *ngIf="msg.errors && (msg.dirty || msg.touched)" 
    class="contact-validate"> 
    <div [hidden]="!msg.errors.required"> 
    Message is required 
    </div>          
</div> 
</div> 
</div> 
<div class="send-msg-btn"> 
    <button type="button" [disabled]="!SendRequest.form.valid" (click)="getRevision()" class="btn btn-primary"><i class="fa fa-send"></i>send message</button>  
</div> 
</form> 

,我號召按鈕點擊getRevision(),試圖
SendRequest.resetForm(); SendRequest.form.reset(); 已經而非工作 請給我建議,這裏有什麼問題。

+0

發送變量「SendRequest」與按鈕點擊事件中的方法調用,getRevision(SendRequest) –

+0

嘿,讓我知道如果[我的解決方案](https://stackoverflow.com/a/45183145/2545680)工作你 –

+0

@Maximus Thans爲答案,我正在檢查它 –

回答

1

您需要使用在你的組件創建的FormGroupreset方法和引用它與this

this.SendRequest.reset(); 
+0

@NikhilRadadiya,Pengyy答案也像這樣'SendRequest.reset(); '沒有指定表單控制值?我不明白它是如何工作的 –

+0

是的,兄弟它做的工作 –

+0

@NikhilRadadiya,嗯,我不明白怎麼做。 'NgForm'甚至沒有'reset'方法[如文檔中所示](https://angular.io/api/forms/NgForm) –

5

您應在單擊事件傳遞SendRequest(形成本身)send message按鈕

<button type="button" [disabled]="!SendRequest.form.valid" (click)="getRevision(SendRequest)" class="btn btn-primary"><i class="fa fa-send"></i>send message</button> 

然後致電form.reset()重置模板驅動表單。

getRevision(SendRequest: NgForm){ 
    ... 
    SendRequest.reset();  // <-------reset form here 
} 

簡單demo

+0

非常感謝,它的工作 –

+0

嘿,你能解釋爲什麼你的解決方案的工作原理? 'NgForm'似乎沒有'reset'方法[如文檔所示](https://angular.io/api/forms/NgForm)。另外,如果你沒有爲它們指定值,爲什麼控件重置爲空?謝謝 –

+0

@Maximus這是HTML5 formElement的普通功能,你可以在這裏找到它的文檔:https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset – Pengyy