Obtaining the selected value of <mat-radio-group> in a typescript file when the SAVE button is clicked

I am facing a situation where I have radio buttons in my HTML code. The radio button options are as follows:

 <mat-radio-group aria-label="Availability for joining" formControlName="availabilityGroup">
          <mat-radio-button value="1" [checked]='true'>Immediate</mat-radio-button>
          <mat-radio-button value="2">1 Month Notice</mat-radio-button>
          <mat-radio-button value="3">3 Month Notice</mat-radio-button>
 </mat-radio-group>

My requirement is to fetch the selected radio button value by the user, and I need to access this value in my typescript file once the user clicks on the SAVE button.

Can someone guide me on how to achieve this?

Answer №1

Utilizing Reactive Forms in your project is a great choice. It appears that you are using a FormGroup to group your form controls, which may be set up similar to the example shown below:

@Component()
export class MyComponent {
  public formGroup: FormGroup = new FormGroup({
    availabilityGroup: new FormControl()
  });
  ...
}

If you need to access the value of a selected radio button within this FormGroup, you can do so using the following code snippet:

this.formGroup.controls.availabilityGroup.value;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular: implementing lazy loading module specifically for development environment (environment.production === false)

I have a lazy loaded module specifically for development purposes and I do not want to include it in the production build. To prevent activation and loading, I have implemented a guard: const routes: Routes = [ { path: 'dev', loadChil ...

When employing a string union, property 'X' exhibits incompatibility among its types

In my code, I have two components defined as shown below: const GridCell = <T extends keyof FormValue>( props: GridCellProps<T>, ) => { .... } const GridRow = <T extends keyof FormValue>(props: GridRowProps<T>) => { ... & ...

Effective strategies for managing form submissions with React and Typescript

As I dive into refactoring my code to TypeScript, especially as I am still getting accustomed to it, I find myself pondering about the HTML element types with React events. This has led me to rethink how I approach form creation and submission event handli ...

Trouble with HTTP.GET Request within a Previous Event Situation

Whenever I attempt to use a previous event to fetch data from my API, the information seems to disappear once outside the subscribe block. I experimented with relocating the service outside of the component, but encountered the same issue. myObject :S ...

Using ng-template in child component from Parent Component in Angular

I am facing a situation where I have a BaseComponent and IncomingRequestsComponent that inherit from BaseComponent. To utilize templates in components that inherit from BaseComponent, I need to include them in the HTML of BaseComponent. export class BaseTi ...

Unmark the checkboxes when the "Anywhere" option is selected

I need a solution in Angular that will automatically uncheck all checkboxes except for "Anywhere in the world" when that option is selected. I've tried various methods but none have worked for my specific case. What is the most straightforward way to ...

What is the relationship between an object and a class in typescript and how can we define

import type { Scene, Camera } from 'three' interface SceneWithActiveCamera extends Scene { activeCamera: Camera } let scene: SceneWithActiveCamera Is there an alternative method to achieve the same result without using an interface? ...

unable to verify identity through a web browser

When I try to launch my Android application built with Ionic 2 on my smartphone, I encounter this error: If you need a tutorial for the application, check out: https://medium.com/appseed-io/third-party-authentication-for-your-ionic-2-mobile-app-9fdd43169d ...

The Material UI Datagrid is failing to display any data

I'm currently facing a challenge that has left me scratching my head. I've implemented Material UI Datagrids, but for some reason, I can't get it to populate with data, and the reason eludes me. Even though the Component profiler shows that ...

Using an Angular HttpClient to authenticate with an API by utilizing a token for

I am currently attempting to fetch data from my Jenkins JSON API via HTTPClient. The data I need access to is restricted, so I must authenticate against Jenkins. To do this, I have generated an API Token. However, I am unsure of how to authenticate myself ...

Issue with Data Table not updating after saving

I have a requirement to refresh my table upon saving. It works fine for a regular table with *ngFor, but I am using the Smartadmin Angular template. I believe the solution involves using table.ajax.reload(), but how can I execute this in an Angular-friendl ...

Automatic Formatting of Typescript in SublimeText

Utilizing Microsoft's Typescript Sublime Plugin, I am able to format a file using the shortcut ^T ^F as outlined in the plugin's feature list. Is there a method to automatically execute this command when saving a file? Similar to the functionali ...

Ensure that the value of a variable in the Ionic/Angular template has been properly initialized

I am currently facing an issue: I have an array of blog posts. Some of them have photos, while others do not. I aim to display the first photo if any are set. How can I verify whether the URL value in my array is set? <ion-header> <ion-navbar& ...

Create a unique TypeScript constant to be mocked in each Jest test

Having difficulty mocking a constant with Jest on a per test basis. Currently, my mock is "static" and cannot be customized for each test individually. Code: // allowList.ts export const ALLOW_LIST = { '1234': true }; // listUtil.ts import { ...

Saving data from a one-to-one relationship using TypeORM and NestJS in a socalled way is a breeze - here's how you

When working with TYPEORM, I have a requirement to create two entities in the database: one for the user and another to store tokens. However, I face a challenge when trying to fill both entities simultaneously during the user creation process. Can someone ...

What could be causing the rendering issue on this React component page when an id is provided in the React Router URL?

These are the dependencies I am using for my React project: "react": "^16.13.1", "react-dom": "^16.13.1", "react-helmet": "^6.1.0", "react-html-parser": "^2.0.2", "react-i ...

Sharing enums between client and server code in Webpack is a smart way to improve

I'm currently working on a project setup that looks like this: | |--public-|file1.ts | |enum.ts | |--server/file2.ts | The issue I am facing is incorporating the enum defined in enum.ts into both file1 and file2. While file1 can import and u ...

Struggling with "Content" not being recognized in Typescript PouchDB transpilation errors?

I have been diligently working on an Ionic app for the past three months with no major issues during development or deployment to mobile devices. However, yesterday I encountered a frustrating NPM dependency problem while trying to deploy to mobile. In an ...

Troubleshooting the unexpected behavior of the shareReply() method in Angular version 15

Utilizing the same API across 3 components can lead to duplicate HTTP calls. To prevent this, I decided to cache the response using shareReply() from RxJs so it can be reused wherever needed. Here's how I implemented it: api-service.ts getUsers(): Ob ...

Experimenting with parallelism using TypeScript/JS

Currently, I am tackling a TS project that involves testing concurrent code and its interactions with a database, specifically focusing on idepotency. My goal is to ensure that multiple requests modifying the same resource will either apply changes correct ...