[英]How to enable or disable the submit button thru radio button in angular
here's the code:这是代码:
list.component.html列表.component.html
<nz-radio-group formControlName="radiostatus" [(ngModel)]="radioValue" (ngModelChange)="onChangeStatus($event)">
<label nz-radio nzValue="passed">Passed</label>
<label nz-radio nzValue="failed">Failed</label>
</nz-radio-group>
<button class="mr-1" nz-button nzType="primary" type="button" [disabled]="disableSubmitBtn()">
<span translate>Submit</span>
</button>
When it select the passed
it should enabled
the submit button if it select the failed
it should disabled the submit button当它选择passed
它应该enabled
提交按钮如果它选择failed
它应该禁用提交按钮
just a small change in your code只是你的代码中的一个小改动
<nz-radio-group formControlName="radiostatus" [(ngModel)]="radioValue" (ngModelChange)="onChangeStatus($event)">
<label nz-radio nzValue="passed">Passed</label>
<label nz-radio nzValue="failed">Failed</label>
</nz-radio-group>
<button class="mr-1" nz-button nzType="primary" type="button" [disabled]="radioValue == 'failed'">
<span translate>Submit</span>
</button>
您只需更改以下行
<button class="mr-1" nz-button nzType="primary" type="button" [disabled]="!radioValue ? 'disabled': null">
You can try like this你可以这样试试
<nz-radio-group formControlName="radiostatus" [(ngModel)]="radioValue" (ngModelChange)="onChangeStatus($event)">
<label nz-radio nzValue="passed">Passed</label>
<label nz-radio nzValue="failed">Failed</label>
</nz-radio-group>
<button class="mr-1" nz-button nzType="primary" type="button" [disabled]="radioValue == 'passed' ? 'false' : 'true'">
<span translate>Submit</span>
</button>
You may asssign true or false value to radio button to identify pass or fail.您可以为单选按钮指定 true 或 false 值以识别通过或失败。 And use this condition to enable or disable button.并使用此条件启用或禁用按钮。
<nz-radio-group formControlName="radiostatus" [(ngModel)]="radioValue" (ngModelChange)="onChangeStatus($event)">
<label nz-radio nzValue="true">Passed</label>
<label nz-radio nzValue="false">Failed</label>
</nz-radio-group>
<button class="mr-1" nz-button nzType="primary" type="button" [disabled]="nzValue" >
<span translate>Submit</span>
</button>
The best and efficient way to achieve this is to use custom validators in angular forms.实现这一目标的最佳和有效方法是以角度形式使用自定义验证器。
import { ValidationErrors, AbstractControl } from '@angular/forms';
export class CustomValidator{
static checkIfPassed (control: AbstractControl): ValidationErrors | null {
if (control.value == 'passed') {
return { shouldbepassed: true }
}
return null;
}
}
Then use this validator in you angular forms.然后在你的角度形式中使用这个验证器。
passed: ['', [Validators.required, checkIfPassed]],
failed: ['', [Validators.required, checkIfPassed]],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.