提交Angular 2后的Clear Modal Form

时间:2016-08-01 07:46:04

标签: typescript angular

如何在提交后清除模态表单值。我想要清除形式的价值,但它不适用于我的事件。我用它来表达清晰。我不知道为什么它不起作用。请帮助我,我会非常感激。 $('#myModal').on('hidden.bs.modal', function () { $(this).find('form')[0].reset(); });

import { Component, Inject } from '@angular/core';
import { NgForm }    from '@angular/forms';
import { FORM_PROVIDERS, ControlGroup, FormBuilder, Validators, Control} from '@angular/common';
import {UserService} from '../../services/user.service';
import {OnInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import { Router }  from '@angular/router';
import { HttpHelper} from '../../utils/httphelper.utils';
import 'rxjs/Rx';
import {InviteModel} from '../../models/invite.model';

import {LocalStorageService} from '../../services/localStorage.service';
@Component({
    selector: 'invite-user',
    templateUrl: '../../app/components/user/invite-user.html',
    providers: [UserService, HttpHelper, LocalStorageService]
})

export class InviteUserComponent {
    InviteUserForm: ControlGroup;
    private _partners: Observable<any[]>;
    private name: string;
    private email: string;
    private partner: number;
    private _data: Observable<any[]>;
    inviteUser: InviteModel;

    constructor(
        @Inject(FormBuilder) public formBuilder: FormBuilder,
        @Inject(UserService) private _userService: UserService,
        @Inject(LocalStorageService) private _localStorageService: LocalStorageService) {

        this.inviteUser = new InviteModel();
        this.InviteUserForm = this.formBuilder.group({
            'username': new Control('', Validators.required),
            'email': new Control('', Validators.compose([Validators.pattern('[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$'), Validators.required])),
            'partner': new Control('', Validators.required)
        });
    }


    Invite(inviteUser) {
        console.log(inviteUser);
        this._userService.inviteUser(inviteUser)
            .subscribe(data => {
                console.log(data);
                this._data = data;
            },
            error => {
                console.log("error while retriving partners");
                this._userService.handleError(error);
            }
        );
    }
}

HTML

&#13;
&#13;
<button type="button" class="btn-u pull-right margin-bottom-10" data-toggle="modal" data-target="#myModal"><span class="fa fa-plus" aria-hidden="true"></span> Invite User</button>

<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Invite User</h4>
            </div>
            <div class="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">
                    </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.-]+\.[a-z]{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">
                            <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>
            </div>

        </div>
    </div>

</div>
&#13;
&#13;
&#13;

0 个答案:

没有答案