formGroup需要一个FormGroup实例。请传入一个。在Angular2中获取错误

时间:2017-08-10 11:49:35

标签: jquery html angularjs html5 angular2-forms

这里我在My Modal模板中使用Anular2 FormGroup即使你得到这个错误。 这是我的表单组然后为什么我收到错误以下是我的总代码

<div class="modal-header">
    <h4 class="modal-title pull-left">{{title}}</h4>
    <button type="button" class="close pull-right" aria-label="Close" (click)="bsModalRef.hide()">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
<div class="modal-body">
        <form class="form-horizontal" novalidate [formGroup]="EmployeeForm">
            <fieldset>
                <div class="form-group" [ngClass]="{'has-error': (EmployeeForm.get('EmpName').touched ||
                                                  EmployeeForm.get('EmpName').dirty) &&
                                                    !EmployeeForm.get('EmpName').valid }">
                    <label for="name">Name</label>
                    <input type="text" class="form-control" formControlName="EmpName" [(ngModel)]="EmpName" />
                    <span class="help-block" *ngIf="(EmployeeForm.get('EmpName').touched ||
                                                         EmployeeForm.get('EmpName').dirty) &&
                                                         EmployeeForm.get('EmpName').errors">
                        <span *ngIf="EmployeeForm.get('EmpName').errors.required">
                            Please enter your first name.
                        </span>
                        <span *ngIf="EmployeeForm.get('EmpName').errors.minlength || EmployeeForm.get('EmpName').errors.maxlength ||
                      EmployeeForm.get('EmpName').pattern">
                            The first name must be longer than A3 and max5 characters.
                        </span>
                    </span>
                </div>

Componet.ts

EmployeeForm: FormGroup;

1 个答案:

答案 0 :(得分:1)

您需要添加 ngIf 条件以检查FormGroup是否具有值

*ngIf="EmployeeForm"