【发布时间】:2017-07-30 22:49:36
【问题描述】:
当电子邮件无效但未在我的 html 中显示时,我尝试显示 md 提示。
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(/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/)) {
return {noEmail: true};
}
}
感谢您的帮助!
【问题讨论】:
-
email模板中的变量是HTMLInputElement实例。您需要使用 FormControl 实例来检查myForm.get('email')之类的错误
标签: angular data-driven angular-forms