[英]Angular 7 - How to disable Save button until Value from Dropdown and field is not populated ? Or all form is not populated?
I already went through disable the button if the input is empty and How Do I disable the button if the input box is empty and enable when field is filled to perform my requirment, but I am using Picklist here. 如果输入为空 , 我已经禁用了该按钮,如果输入框为空,我如何禁用该按钮 , 如何在填充字段以执行我的要求时启用该按钮 ,但是我在这里使用的是Picklist。 There how can I make sure picklist and fields is not populated ? 我该如何确保未填充选择列表和字段? Until I really don't want to show add button. 直到我真的不想显示添加按钮。
<div class="zzz-input">
<label class="zzz-inputbox-label" for="">
Patient Role
</label>
<div class="input-container">
<select id="" class="input-box" type="number" aria-required="true" [(ngModel)]="patient.roleTypeId"
name="roleTypeId">
<option *ngFor="let patientRole of patientRoles" value="{{patientRole.patientRoleId}}">{{patientRole.patientRoleName}}
</option>
</select>
</div>
</div>
<div class="zzz-input">
<label class="zzz-inputbox-label" for="">
Patientnt Name
</label>
<div class="input-container">
<input type="text" id="" class="input-box" aria-required="true" minlength="0" maxlength="100" autocomplete="off"
width="0" min="" max="" step="" [(ngModel)]="patient.patientId" name="patientId">
</div>
</div>
<div>
<button class="zzz-btn zzz-btn-primary" title="Submit" aria-label="Save" (click)="addPatients()">
Add
</button>
</div>
This is why angular forms exist. 这就是为什么存在角形的原因。 I suggest to read the documentation on forms. 我建议阅读表格上的文档 。 I further suggest you dig into reactive forms, but here you are using ngModel, so let's make this form template driven. 我进一步建议您深入研究响应式表单,但是这里您使用的是ngModel,因此让我们将此表单模板驱动。 Just wrap your code inside form tags and make fields required. 只需将您的代码包装在表单标签中,然后将字段设为必填即可。 Here we name the form simply f
. 在这里,我们将形式简单地命名为f
。 Then disable the button with !f.valid
or if you want to actually hide the button you can use *ngIf="f.valid"
. 然后使用!f.valid
禁用按钮,或者如果您想真正隐藏按钮,则可以使用*ngIf="f.valid"
。 So here's a shortened modified code, where we disable the button if the form is not valid: 因此,这是一个简短的修改后的代码,如果表单无效,我们将在其中禁用按钮:
<form #f="ngForm" (ngSubmit)="onSubmit(f.value)">
<select [(ngModel)]="patient.roleTypeId" name="roleTypeId" required>
<! -- .... --->
</select>
<input [(ngModel)]="patient.patientId" name="patientId" required>
<button [disabled]="!f.valid">
Add
</button>
</form>
Full code in STACKBLITZ STACKBLITZ中的完整代码
Use this - 用这个 -
<button class="zzz-btn zzz-btn-primary" title="Submit" aria-label="Save"
[disabled]="patient.patientId && patient.roleTypeId" (click)="addPatients()">
Add
</button>
Hope it help! 希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.