gpt4 book ai didi

javascript - FormBuilder Angular 4中的禁用按钮

转载 作者:行者123 更新时间:2023-11-30 08:25:01 26 4
gpt4 key购买 nike

我在 Angular 4 中使用 FormBuilder,当没有填写所有字段时,我需要禁用提交按钮。

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>

现在按钮是事件的,那是不对的

enter image description here

最佳答案

使用 disabled 属性检查表单验证状态

<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>

关于javascript - FormBuilder Angular 4中的禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47180179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com