我有一个输入用户名和密码的表格,像这样。
<mat-form-field class="mdb-form-field-modal form-adjustments">
<input (keydown)="emailBtnFocus($event)" tabindex="0" matInput placeholder="username" formControlName="username">
<mat-error *ngIf="username.hasError('email') && !username.hasError('required')">
{{ signInEmailErrorText }}
</mat-error>
<mat-error *ngIf="username.hasError('required')">
{{requiredError }}
</mat-error>
</mat-form-field>
export class SignInComponent implements OnInit {
signInForm: FormGroup;
username = new FormControl('', [
Validators.required, Validators.email
]);
ngOnInit() {
this.authState = this.store.select('auth');
this.signInForm = new FormGroup({
'username': this.username,
'password': this.password,
'staySignedIn': this.staySignedIn
});
}
// Submit Logic.
onSignInSubmit() {
console.log('SignInForm:', this.signInForm);
//logic for submit goes here
}
}
这样做。
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
export class SignInComponent implements OnInit {
signInForm: FormGroup;
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.authState = this.store.select('auth');
this.signInForm = this.formBuilder.group({({
'username': ['', [Validators.required, Validators.email]],
'password': ['', Validators.required],
'staySignedIn': ['']
});
}
// Submit Logic.
onSignInSubmit() {
console.log('SignInForm:', this.signInForm);
//logic for submit goes here
}
}