Why are my radio buttons being selected automatically? Is there a way to set just one as the default option in Angular 9?

I am currently facing an issue with two radio buttons. What I want is for the admin radio button to display one form when clicked, and the user radio button to display another form. Currently, both are checked by default and I am unsure why. I only want one to be checked initially, showing one form at a time.

  • Below, I will provide the code for the radios and an image of them. I am utilizing angular-material.

Radio button's HTML

<mat-radio-group aria-label="Select an option" class="center">
    <mat-radio-button  (click)="radio_btn=true" >Admin</mat-radio-button>
    <mat-radio-button (click)="radio_btn=false">User</mat-radio-button>
  </mat-radio-group>
  <div class="ng-container ">
        <div class="row justify-content-center" *ngIf="radio_btn==true">
        <form class="example-form " [formGroup]="signupForm" (ngSubmit)="send(signupForm)">
          <mat-form-field class="example-full-width">
            <input matInput formControlName="email" placeholder="User" type="email">
          </mat-form-field><br>
          <div *ngIf="signupForm.get('email').hasError('email') && signupForm.get('email').touched">Introduce an email</div><br>
          <mat-form-field class="example-full-width">
            <input matInput formControlName="password" placeholder="Password" type="password">
          </mat-form-field><br>
          <div *ngIf="signupForm.get('password').hasError('password') && signupForm.get('password').touched">Introduce the correctly password</div><br>
          <button mat-raised-button [disabled]="signupForm.invalid" class="colour_button " type="submit">Register</button>
        </form>
    </div>
</div>

TS file for the radios

export class HomeComponent implements OnInit {
  radio_btn:boolean = true;
  ngOnInit(): void {
  }
}

This is how they appear:

https://i.sstatic.net/VcOIg.png

Answer №1

Place the following code inside your radio group component:

<mat-radio-group [(ngModel)]="selected_radio_btn"

Next, include:

<mat-radio-button [value]=“true“

And

<mat-radio-button [value]=“false”

Remember to remove the (click) event handlers from the radio buttons.

To update the selected value of the radio button.

Refer to the Material radio example html page.

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

My Angular component seems to have a mind of its own, constantly refreshing itself and causing me

I'm facing an issue with a view that loads another component using routing. Here is the code that triggers the loading: <a href="/productDetails/{{product.ProductId}}">{{product.Name}}</a> Below is the div where the other component (prod ...

Angular 5 update causing 'NgModule not found' error

After attempting to upgrade my app to Angular 5, I encountered a series of bugs. Among them, there is one persistent issue that has me stumped. An uncaught error has surfaced: 'Unexpected value HttpClient imported by the module AppModuleShared. Ple ...

Angular 2 forms are displaying ngvalid when input fields are marked as nginvalid

The form displays ngvalid because I have included the code like this: <form novalidate class="pop-form" (submit)="signUp()" #regForm="ngForm"> <div class="group"> <input type="text" [(ngModel)]="signUpData.name" [ngMode ...

Access a static class property through an instance

New and Improved Question: As I develop a client-side application, I am structuring an event-handling system inspired by the Redux framework. I have defined different event types as subclasses of a custom Event class. Each subclass includes its own stat ...

Tips for performing a cast when initializing a new object using TypeScript

Is there a way to create a new object using the map function on employees in TypeScript without declaring a variable? employees.map(x=> ({name: x.firstName})); This way, I can have VSCode intellisense while making my new object. However, if I try em ...

Tips for developing a personalized forms validator with the help of a service

I have developed an Injectable Service to create a custom Form Validator: import { Injectable } from '@angular/core' import { FormControl } from '@angular/forms' import { Observable, of, timer } from 'rxjs' imp ...

What is the proper way to access and modify the child component within a parent component?

I am working with nested components as shown below: app.parent.component > app.container.component > app.containeritem.component Here is an example: app.parent.component import ... @Component({ selector:'parent', template: &apos ...

Adjust property value based on changes in a related property

Currently, I am developing a TypeScript-powered Angular (5) application and I have encountered a puzzling question that is proving elusive to solve. Let me illustrate with the following example: ... export class SomeComponent implements onInit { ... ...

Guide to adding npm semver to an Angular project in Ionic

Currently, I am working on an Angular project that utilizes the Ionic framework. My goal is to employ Semvar to compare the local version with the API version in order to prompt users to update. Following the installation of semver: npm install semver I ...

What is the process for converting a .ts file to a .js file within a Next.js project for Web worker implementation?

I am currently working on a TypeScript Next.js project: "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint& ...

Compelled to utilize unfamiliar types in TypeScript generics

When working with a Typescript React form builder, I encountered a situation where each component had different types for the value and onChange properties. To tackle this issue, I decided to utilize generics so that I could define the expected types for e ...

Implementing JWT authentication in a Spring backend and an Angular frontend without a specified header

I'm currently working on implementing JWT token authentication using spring boot and angular. Upon successful login, a bearer token is generated, however, in the JWTAuthorizationFilter, I am encountering a null header which results in it returning ano ...

Looking for a way to convert 24-hour format to minutes in Angular? I'm in need of some TypeScript code to

I am looking for Typescript code that can convert 24-hour time format into minutes. For example, when converting 1.00 it should output as 60 minutes. When converting 1.30 it should equal to 90 minutes. If anyone has the code for this conversion, p ...

React Typescript Issue: Antd Checkbox value not updating correctly

Hey there, I'm having trouble filling the Checkbox value with my state data as it's not accepting it. Here is the Checkbox Code: <Form.Item label="Active"> <Checkbox value={this.state.selectedRecord.active} /> </Form ...

How to trigger a click event in React using TypeScript and material-ui library

Currently, I am facing an issue when trying to update the value of material-ui TextFields from the store. When manually typing inside the field, everything works fine as expected with the handleChange() and handleBlur() functions handling the events. Howev ...

Express is having trouble rendering the static index file

I am currently working on an Angular application, where my goal is to serve the build files from the Angular application using the dist directory in my express server. In order to achieve this, I am copying the files generated by ng build and pasting them ...

Authorization error encountered with Angular4 and NGINX

I'm facing difficulties running my Angular 4 application with nginx. Upon checking the error.log for nginx, I came across this message: 2018/04/21 12:49:52 [error] 13961#13961: *1 open() "/root/test/myapp/web-portal/index.html" failed (13: Perm ...

Ignore tslint in method chains using the tslint-disable comment

I have a scenario where I populate an Array and then use it to map onto a sequence of React components return ( <div className={style}> <Header title="INVENTORY" /> {Array(slots) .fi ...

Setting innerHTML does not affect the content of an SVG element

I am currently working on an Angular 7 application and I need to dynamically update the text value based on a dropdown selection. For example, if the id of the text element is 10, then I want to change the text from 'hi' to 'hello'. T ...

Issue encountered when attempting to render a fully dynamic table with an empty object

I'm looking to dynamically generate a table with headers, rows, and cells based on JSON data retrieved from an API call. The data being fetched from the API is in the form of a dataframe. Here is a sample of the JSON'd dataframe (as seen in the ...