0

here's the code:

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

4
  • also you are using formControlname and ngModel together which is not the correct way. you should use one. Commented Dec 12, 2019 at 5:10
  • @Mridul if I remove the formControlname and ngModel there will be ann error Commented Dec 12, 2019 at 5:22
  • You need to keep one based on your requirement :) Commented Dec 12, 2019 at 5:23
  • okay thanks.. @Mridul Commented Dec 12, 2019 at 5:27

5 Answers 5

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

how'bout when you apply it on ts?
in that case you need to take a variable on your ts file example: isDisabled: boolean; and on your html [disabled]="isDisabled" by default it will be false. then under ngModelChange function you need to check the radio button value if radioValue is failed then you can make isDisabled = true;
2

You just change following line

 <button class="mr-1" nz-button nzType="primary" type="button" [disabled]="!radioValue ? 'disabled': null">

Comments

1

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>

Comments

1

You may asssign true or false value to radio button to identify pass or fail. 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>

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.