Tips for adjusting the radio button value similarly to a checkbox in Angular 2 using *ngFor

my checkbox and radio button implementation:

<input id="{{k.group_name}}_{{i}}" name="{{k.group_name}}" type="checkbox" class="hide" name="{{k.group_name}}" [value]="m.details" (change)="change($event, m , k.item_ingredient_group_key,false,k.maximum)">

Similarly for radio button:

<input id="{{k.group_name}}_{{i}}" type="radio" class="hide" name="{{k.group_name}}" [value]="m.details" (change)="change($event, m , k.item_ingredient_group_key,true,k.maximum)">

Within my json service, I retrieve either is_selected = true or is_selected = false based on the JSON data provided.

The value of is_selected needs to be updated accordingly.

For checkboxes, I have used binding with [(ngModel)]="is_selected", which works as expected. However, when it comes to radio buttons, the updating of values does not function similarly to checkboxes.

There seems to be a discrepancy in how the true and false values are managed between the two types of input elements.

Answer №1

Begin by properly closing your input tags:

//before
    <input id="{{k.group_name}}_{{i}}" name="{{k.group_name}}" type="checkbox" class="hide" [value]="m.details" (change)="change($event, m , k.item_ingredient_group_key,false,k.maximum)">

//after
<input id="{{k.group_name}}_{{i}}" name="{{k.group_name}}" type="checkbox" class="hide" [value]="m.details" (change)="change($event, m , k.item_ingredient_group_key,false,k.maximum)" />

Next, ensure that you only have one 'name' attribute in your checkbox input:

//Two Name tags, what for?
    <input id="{{k.group_name}}_{{i}}" name="{{k.group_name}}" type="checkbox" class="hide" [value]="m.details" (change)="change($event, m , k.item_ingredient_group_key,false,k.maximum)">

Last but not least:

Consider using the 'checked' attribute for your radio button:

<input id="{{k.group_name}}_{{i}}" type="radio" class="hide" name="{{k.group_name}}" [value]="m.details" [checked]="change($event, m , k.item_ingredient_group_key,true,k.maximum)" />

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

Uncovering the mystery of retrieving form values from dynamic HTML in Angular 2

As a beginner in Angular 2, I am facing challenges extracting values from Dynamic HTML. My task involves having some Form Inputs and injecting Dynamic HTML within them that contain additional inputs. I followed the example by @Rene Hamburger to create the ...

How to extract a specific Observable<T> from an Observable<T[]> array in RxJS

When studying Angular, I created a simulated web service that accomplishes two tasks: It provides an Observable of an array (Observable<T[]>) It offers an item identified by ID as an Observable (Observable<T>) This is the approach I used: e ...

Continue looping in Javascript until an empty array is identified

Currently, I am in search of a solution to create a loop in Javascript that continues until the array of objects is empty. The object I am working with looks like this: "chain": { "evolves_to": [{ "evolves_to": [{ ...

Tips on how to retrieve a stubbed Observable<void> in RxJS

Currently, I am attempting to stub an API and would like to retrieve a stubbed response from my Service. The method in my service appears as follows: public addItem(item): Observable<void> { this.listOfItems.push(item); return of(); } As f ...

Error encountered when upgrading to Material-UI v5 rc.0 with typescript

After updating my material-ui to version v5-rc.0, I decided to implement styled-components. However, as I was working on my Component.styles.ts file, I encountered an error: The inferred type of 'StyledStepper' cannot be named without a referen ...

Module '../../third_party/github.com/chalk/supports-color' not found in the directory

Within my tutoring-frontend-main project folder There is a file named package.json { "name": "app-frontend", "version": "0.0.0", "license": "MIT", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build --prod", "test": "n ...

Troubleshooting the need for refreshing in an Angular application when selecting the same menu option repeatedly

Issue with Angular Application: I've noticed a problem in my Angular app where clicking the menu within a component does not trigger a refresh or reload of the component. I want to make sure that the component reloads whenever its specific menu is cl ...

How do I insert a new column into the result set of a query in Prisma?

Imagine a scenario where there are two tables: User, which has fields for name and Id, and Post, which has fields for name and content. These tables are connected through a many-to-many relationship (meaning one post can have multiple users/authors and eac ...

Make changes to an array in Javascript without altering the original array

I currently have an array : let originalArr = ['apple', 'plum', 'berry']; Is there a way to eliminate the item "plum" from this array without altering the originalArr? One possible solution could be: let copyArr = [...origin ...

What are the steps for importing a file into a React app that is built using Create React App as plain text?

Objectives I aim to showcase code snippets from the project itself for reference. I intend to keep the displayed code up-to-date with its implementation. I prefer not to remove myself from create-react-app This React project, built using create-react-ap ...

Universal loading screen across all components

I am currently implementing a loading screen for this component in conjunction with the fetch method. My concern is whether I will need to replicate the same loading logic used in the example for every component that utilizes the fetch() method, or if the ...

Utilizing Typescript generics with an optional second parameter

Learning about generics in typescript has been quite challenging for me. However, I was able to make it work successfully. export type Events = { LOGIN: undefined NAVIGATION: { screen: string } SUPPORT: { communication_method: 'chat&ap ...

When defining a stripe in TypeScript using process.env.STRIPE_SECRET_KEY, an error of "string | undefined" is encountered

Every time I attempt to create a new stripe object, I encounter the error message "Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string& ...

Error during Next.js build: Incompatible types - cannot assign type to 'never'

Encountering an error in the console while attempting to build my project: .next/types/app/facebook/page.ts:8:13 Type error: Type 'OmitWithTag<typeof import("D:/projects/abkh24/client/app/facebook/page"), "metadata" | "defa ...

Obtaining the attribute value of a disabled element in an Angular JS application

Currently, I am struggling to retrieve the attribute value of a disabled element during runtime and then assert its value. The code I'm using is not providing the desired result: softAssert.assertFalse(shrSub.nextButton().waitForPresent().getAttribu ...

Sharing data across multiple components while navigating in Angular is a common requirement. By passing

I have a specific structure set up in my application: <div class="container"> <top-navbar></top-navbar> <router-outlet></router-outlet> <navbar></navbar> </div> The components I have include Top ...

Angular application triggering multiple subscribe method calls upon a link click event

Here is the code for my navbar component: <li *ngFor="let item of menu"> <a *ngSwitchCase="'link'" routerLinkActive="active" [routerLink]="item.routerLink" (click)="Navigation(item.title)&q ...

Angular 14: Creating a Dynamic Component with NgControl as a Dependency

I am currently trying to dynamically add a component to the DOM using ViewContainerRef's createComponent method. The challenge I am facing is that the component relies on NgControl, and my ultimate goal is to incorporate these components into a form u ...

The validation of DOM nesting has detected that a <td> element cannot be placed within an <a> element

When working on a React project with Material UI, I encountered an issue while trying to create a table. My goal was to make the entire row clickable, directing users to a page with additional information on the subject. Below is the snippet of code for th ...

Converting numbers to strings based on locale in React Native

I have a quantity that, when using the amount.toFixed() function, produces the string "100.00". I would like this result to be formatted according to the specific locale. For example, in Italian the desired output is 100,00, while in English it should be ...