0

I am having a radio button group with multiple values. One value is a custom value which needs to be set by an field.

HTML:

<mat-radio-group [(ngModel)]="timeout">
 <mat-radio-button value="1">1 hour</mat-radio-button>
 <mat-radio-button value="6">6 hours</mat-radio-button>
 <mat-radio-button value="{{timer_cust}}">Custom</mat-radio-button>
</mat-radio-group>

<mat-form-field>
 <input matInput type="number" placeholder="min" [(ngModel)]="timer_cust">
</mat-form-field>

When I check the radio "Custom", the correct value is reflected on "timeout" variable. But when I change the input value, "timer_cust" updates, but "timeout" sticks to the old value. How do I update "timeout" which is bound to the "mat-radio-group" as the input value changes?

1
  • Are you talking about radio group value ("timeout") change after you type custom value equal to e.g. 1 hour? Commented Aug 11, 2018 at 9:23

1 Answer 1

1

Here timer_cust value changes not visible for timeout. So my suggestion is use the formControl for get matInput value changes.

<mat-radio-group [(ngModel)]="timeout">
    <mat-radio-button value="1">1 hour</mat-radio-button>
    <mat-radio-button value="6">6 hours</mat-radio-button>
    <mat-radio-button [checked]="isChecked" [value]="formControl.value">Custom</mat-radio-button>

    <mat-form-field>
        <input matInput type="number" placeholder="min" [formControl]="formControl">
    </mat-form-field>
</mat-radio-group>

Then you can observe value changes by fromcontrol and override that value from timeout variable.

this.formControl.valueChanges.subscribe((value) => {
      if (value) {
        this.timeout = value;
        this.isChecked = true; // for auto select custom ratio option when changing input value
      }
    });

Here I attached stackblitz link for you further infomation. https://stackblitz.com/edit/angular-fugo9e?file=styles.css

Sign up to request clarification or add additional context in comments.

1 Comment

that's working fine. how do I modify if there are multiple input fields? do I need to create a formControl for each input field?

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.