简体   繁体   English

在角度2中清除ng2-bs3-modal形式

[英]clear ng2-bs3-modal form in angular 2

I am using ng2-bs3-modal in my angular 2 app. 我在我的angular 2应用程序中使用ng2-bs3-modal。 Now I want to clear all forms feilds to be clear with close button is click. 现在,我要清除所有表单字段,单击“关闭”按钮即可清除。 how to do this in best and easy way. 如何以最佳和简便的方式做到这一点。

html html

 <button type="button" class="btn-u pull-right margin-bottom-10" (click)="modal.open()"><span class="fa fa-plus" aria-hidden="true"></span> Invite User</button> <modal #modal> <modal-header [show-close]="true"> <h4 class="modal-title">Invite User</h4> </modal-header> <modal-body> <form class="form-horizontal" id='myForm' role="form" [ngFormModel]="InviteUserForm"> <div class="input-group margin-bottom-20"> <span class="input-group-addon"><i class="fa fa-user"></i></span> <input type="text" [(ngModel)]='inviteUser.username' class="form-control" ngControl="username" required> </div> <div class="input-group margin-bottom-20"> <span class="input-group-addon"><i class="fa fa-envelope"></i></span> <input type="email" required [(ngModel)]='inviteUser.email' class="form-control" ngControl="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\\.[az]{2,3}$"> </div> <div class="input-group margin-bottom-20"> <span class="input-group-addon"><i class="fa fa-glass"></i></span> <select [(ngModel)]='inviteUser.partnerId' class="form-control" ngControl="partner" required> <option>Select one</option> <option *ngFor="let partner of _partners" value={{partner.Id}}> {{partner.Name}} </option> </select> </div> </form> <button type="button" class="btn-u btn-u-default margin-right-5" data-dismiss="modal">Close</button> <button type="button" [disabled]="!InviteUserForm.valid" class="btn-u pull-right" (click)="Invite(inviteUser)" data-dismiss="modal">Invite</button> </modal-body> </modal> 

Handle yout close button event and reinitialize the form object so you will do something like : 处理关闭按钮事件并重新初始化表单对象,以便执行以下操作:

<button type="button" (click)="resetForm()" class="btn-u btn-u-default margin-right-5" data-dismiss="modal">Close</button>

and in your ts file : 并在您的ts文件中:

resetForm(){
    this.inviteUser = new InviteUserForm(); // this will clear all fields in bound in html
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM