我是角度的新手,可以说,我有如下反应形式
ngOnInit() {
this.registerFormGroup = this.formBuilder.group({
email: [ '', Validators.compose([Validators.required, Validators.email])],
password: this.formBuilder.group({
first: [ '', Validators.required ],
second: [ '', Validators.required, ]
})
});
}
我的角度模板看起来像
<div class="container">
<form [formGroup]="registerFormGroup"
(ngFormSubmit)="registerUser(registerFormGroup.value)" novalidate>
<div class="form-group" >
<label for="email">Email</label>
<input type="email" formControlName="email" placeholder="Enter Email"
class="form-control">
</div>
<div *ngIf="!registerFormGroup.get('password').get('first').valid"
class="alert alert-danger">
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-success btn-lg"
[disabled]="!registerFormGroup.valid">Submit</button>
</div>
例如,电子邮件字段有两个验证,例如required
和email
类型验证所以取决于我必须显示错误消息的错误,所以在我的模板中使用类似
<div *ngIf="!registerFormGroup.get('email').valid && (registerFormGroup.get('email').touched)"
class="alert alert-danger">
</div>
registerFormGroup.get('email')
我没有一次又一次地添加相同的内容,而是尝试创建模板表达式#emailForm="registerFormGroup.get('email')"
,如
<input type="email" formControlName="email" placeholder="Enter Email" class="form-control" #emailForm="registerFormGroup.get('email')">
这样我就可以使用 like
<div *ngIf="!emailForm.valid" class="alert alert-danger">
</div>
但我收到了类似的错误
compiler.es5.js:1690 未捕获错误:模板解析错误:没有将“exportAs”设置为“registerFormGroup.get('email')”的指令(“l> ]#emailForm="registerFormGroup.get('email' )">
我犯了什么错误??