【发布时间】:2021-05-27 08:45:16
【问题描述】:
我有一个这样的简单输入表单
<form class="form" [formGroup]="registerForm" (ngSubmit)="onSubmit()">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="floatEmail">Product Name</label>
<input type="text" class="form-control" id="floatEmail" name="floatEmail" formControlName="name" >
<small class="form-text text-muted danger" *ngIf="f.name.errors.required" >Please enter a name!</small>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-right">
<button type="button" class="btn btn-success btn-raised mr-1">Cancel</button>
<button type="button" class="btn btn-danger btn-raised" >Submit</button>
</div>
</div>
</form>
我只想验证和控制值。
.ts
registerForm: FormGroup;
ngOnInit() {
this.registerForm = this.formBuilder.group({
name: ['', Validators.required],
});
}
get f() { return this.registerForm.controls; }
onSubmit() {
this.submitted = true;
if (this.registerForm.invalid) {
return;
}
alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.registerForm.value, null, 4));
}
问题总是显示错误我想在点击后显示错误。
console.log 也显示这个错误
TypeError: Cannot read property 'required' of null
【问题讨论】:
标签: angular typescript