当电子邮件无效但它不显示在我的html中时,我试图显示一个md-hint。不使用数据驱动表格中的电子邮件验证器显示md-hint
HTML
<form [formGroup]="myForm" (ngSubmit)="onSignup()">
<md-input-container class="col-md-6 md-icon-left">
<md-icon class="material-icons">mail_outline</md-icon>
<input formControlName="email" mdInput #email type="email" name="email" class="form-control" placeholder="Email">
<md-hint *ngIf="!email.pristine && email.errors != null && email.errors['noEmail']">Invalid mail address</md-hint>
</md-input-container>
</form>
component.ts
myForm: FormGroup;
error = false;
errorMessage = '';
constructor(private fb: FormBuilder,
private authService: AuthService,
rv: RutValidator) {
this.myForm = this.fb.group({
email: ['', Validators.compose([
Validators.required,
this.isEmail
])],
});
}
isEmail(control: FormControl): {[s: string]: boolean} {
if (!control.value.match(/^\[email protected][a-zA-Z_]+?\.[a-zA-Z]{2,3}$/)) {
return {noEmail: true};
}
}
感谢您的帮助!
'在你的模板email'变量是'HTMLInputElement'实例。你需要使用FormControl实例来检查错误,比如'myForm.get('email')' – yurzui