如何禁用提交按钮,直到表格在Angular 12中有效?
login.component.html
<form (ngSubmit)="submitLogin()" #loginForm="ngForm">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="email" name="email" [(ngModel)]="form.email" [ngModelOptions]="{standalone: true}" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="password" name="password" [(ngModel)]="form.password" [ngModelOptions]="{standalone: true}" required>
</div>
<button type="submit" class="btn btn-primary" [disabled]="!loginForm.form.valid">Submit</button>
</form>
我是Angular的新手,我试图禁用提交按钮,直到表单输入字段为空白,但在此显示启用,但如果我像“ loginform.form.form.form.form.valid”编码,然后提交按钮显示put
!
喜欢“!loginform.form.valid”
然后再次显示启用提交按钮。那么,如何解决此问题?请帮我。
谢谢
login.component.html
<form (ngSubmit)="submitLogin()" #loginForm="ngForm">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="email" name="email" [(ngModel)]="form.email" [ngModelOptions]="{standalone: true}" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="password" name="password" [(ngModel)]="form.password" [ngModelOptions]="{standalone: true}" required>
</div>
<button type="submit" class="btn btn-primary" [disabled]="!loginForm.form.valid">Submit</button>
</form>
I am new in angular and I trying to disabled submit button until form input field is blank but here it showing enable but if I code like "loginForm.form.valid"
then the submit button show disabled after put !
like "!loginForm.form.valid"
then again it show enable submit button. So, How can I fix this issue? Please help me.
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法1:
尝试 [disabled] =“!
并添加如下所示的禁用CS。
方法2
在html使用中使用get方法
[disabled] =“!form.valid”
Method 1:
Try [disabled]="!loginForm.valid" dont use form key word.
And add disabled css like below.
Method 2
Use get method in component
In HTML use [disabled]="!form.valid"
如果其他答案不起作用,请尝试一下。
Try this if the other answers don't work.
您可以将其绑定到函数以使其自身更新。
模板:
TS:
You can bind it to a function to make it update itself.
Template:
Ts:
尝试 -
或
Try to -
or