Angular 4 Reactive Forms FormControl 错误为空
Angular 4 Reactive Forms FormControl errors is null
如果我在没有输入任何内容的情况下切换文本输入,则会显示错误消息 divs,表明所需的验证器已正确触发。但是,如果我在其中一个字段中键入任何内容,控制台会立即抛出此错误:
Cannot read property 'required' of null
这是我的组件:
import { PasswordValidators } from './../validators/password.validators';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators, FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-changepassword-form',
templateUrl: './changepassword-form.component.html',
styleUrls: ['./changepassword-form.component.css']
})
export class ChangepasswordFormComponent {
form;
constructor(fb: FormBuilder) {
this.form = fb.group({
newPassword: ['', Validators.required],
confirmPassword: ['', Validators.required]
})
}
get newPassword() {
return this.form.get('newPassword');
}
get confirmPassword() {
return this.form.get('confirmPassword');
}
}
和HTML:
<form [formGroup]="form">
<div class="form-group">
<label for="newPassword">New Password</label>
<input formControlName="newPassword" id="newPassword" type="text" class="form-control">
<div class="alert alert-danger" *ngIf="newPassword.touched && newPassword.errors.required">
Required
</div>
</div>
<div class="form-group">
<label for="confirmPassword">Confirm Password</label>
<input formControlName="confirmPassword" id="confirmPassword" type="text" class="form-control">
<div class="alert alert-danger" *ngIf="confirmPassword.touched && confirmPassword.errors.required">
Required
</div>
</div>
<p><button class="btn btn-primary">Submit</button></p>
</form>
错误来自于:
*ngIf="newPassword.touched && newPassword.errors.required"
当您输入一个值时,不再有错误集合,因此检查 newPassword.errors.required
会生成 Cannot read property 'required' of null
错误。
试试这样的方法:
*ngIf="newPassword.touched && newPassword.errors?.required"
举个例子,我的是这样的:
<div class="col-md-8">
<input class="form-control"
id="productNameId"
type="text"
placeholder="Name (required)"
required
minlength="3"
[(ngModel)] = product.productName
name="productName"
#productNameVar="ngModel" />
<span class="help-block" *ngIf="(productNameVar.touched ||
productNameVar.dirty || product.id !== 0) &&
productNameVar.errors">
<span *ngIf="productNameVar.errors.required">
Product name is required.
</span>
<span *ngIf="productNameVar.errors.minlength">
Product name must be at least three characters.
</span>
</span>
</div>
您也可以使用以下语法,无需先输入值即可使用:
form.get('newPassword').hasError('required')
这将检查错误中的 'required'。
这样也行,而且更简洁:
form.hasError('required','newPassword')
如果您使用 AOT 选项进行编译,根据此 article,您应该特权 hasError() 语法:
Don’t use control.errors?.someError, use control.hasError(‘someError’)
如果我在没有输入任何内容的情况下切换文本输入,则会显示错误消息 divs,表明所需的验证器已正确触发。但是,如果我在其中一个字段中键入任何内容,控制台会立即抛出此错误:
Cannot read property 'required' of null
这是我的组件:
import { PasswordValidators } from './../validators/password.validators';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators, FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-changepassword-form',
templateUrl: './changepassword-form.component.html',
styleUrls: ['./changepassword-form.component.css']
})
export class ChangepasswordFormComponent {
form;
constructor(fb: FormBuilder) {
this.form = fb.group({
newPassword: ['', Validators.required],
confirmPassword: ['', Validators.required]
})
}
get newPassword() {
return this.form.get('newPassword');
}
get confirmPassword() {
return this.form.get('confirmPassword');
}
}
和HTML:
<form [formGroup]="form">
<div class="form-group">
<label for="newPassword">New Password</label>
<input formControlName="newPassword" id="newPassword" type="text" class="form-control">
<div class="alert alert-danger" *ngIf="newPassword.touched && newPassword.errors.required">
Required
</div>
</div>
<div class="form-group">
<label for="confirmPassword">Confirm Password</label>
<input formControlName="confirmPassword" id="confirmPassword" type="text" class="form-control">
<div class="alert alert-danger" *ngIf="confirmPassword.touched && confirmPassword.errors.required">
Required
</div>
</div>
<p><button class="btn btn-primary">Submit</button></p>
</form>
错误来自于:
*ngIf="newPassword.touched && newPassword.errors.required"
当您输入一个值时,不再有错误集合,因此检查 newPassword.errors.required
会生成 Cannot read property 'required' of null
错误。
试试这样的方法:
*ngIf="newPassword.touched && newPassword.errors?.required"
举个例子,我的是这样的:
<div class="col-md-8">
<input class="form-control"
id="productNameId"
type="text"
placeholder="Name (required)"
required
minlength="3"
[(ngModel)] = product.productName
name="productName"
#productNameVar="ngModel" />
<span class="help-block" *ngIf="(productNameVar.touched ||
productNameVar.dirty || product.id !== 0) &&
productNameVar.errors">
<span *ngIf="productNameVar.errors.required">
Product name is required.
</span>
<span *ngIf="productNameVar.errors.minlength">
Product name must be at least three characters.
</span>
</span>
</div>
您也可以使用以下语法,无需先输入值即可使用:
form.get('newPassword').hasError('required')
这将检查错误中的 'required'。
这样也行,而且更简洁:
form.hasError('required','newPassword')
如果您使用 AOT 选项进行编译,根据此 article,您应该特权 hasError() 语法:
Don’t use control.errors?.someError, use control.hasError(‘someError’)