I use FormBuilder in Angular 4 and i need to disable submit button, when not filled all the fields.
component.ts
public loginForm = this.fb.group({
email: ['', Validators.required],
password: ['', Validators.required],});
constructor(
public fb: FormBuilder
) { }
component.html
<form [formGroup]="loginForm" (ngSubmit)="doLogin($event)">
<input formControlName="email" type="email" placeholder="Your email">
<input formControlName="password" type="password" placeholder="Your password">
<button type="submit">Log in</button>
</form>
And now button is active, that is not right
您需要明确地说:
<button type="submit" [disabled]="loginForm.invalid">Log in</button>
Use disabled property with checking form validation state
<form [formGroup]="loginForm" (ngSubmit)="doLogin($event)">
<input formControlName="email" type="email" placeholder="Your email">
<input formControlName="password" type="password" placeholder="Your password">
<button type="submit" [disabled]="!loginForm.valid">Log in</button>
</form>
<form [formGroup]="loginForm" (ngSubmit)="doLogin($event)">
<input formControlName="email" type="email" placeholder="Your email">
<input formControlName="password" type="password" placeholder="Your password">
<button type="submit" [disabled]="loginForm.invalid || loginForm.pristine">Log in</button>
</form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.