What is the best way to retrieve the value from a chosen radio button?

Here is the HTML code snippet:

    <ion-list radio-group [(ngModel)]="portion" (ionChange)="getPortionType()">
        <ion-list-header>
            Select Portion
        </ion-list-header>
        <ion-item *ngFor="let item of portionList">
            <ion-label>{{item.portion}}
                <p>{{item.price}} ₺</p>
            </ion-label>
            <ion-radio value="{{item.id}}"></ion-radio>
        </ion-item>
        <button *ngIf="portion != ''" ion-button block (click)="proceedToNextStep()">Proceed</button>

    </ion-list>

You can see that I am using [(ngModel)] = "portion". When I retrieve this value, only the record ID is available. However, I want to access all the values from the selected radio button.

I specifically need to use {{item.portion}} and {{item.price}}. Please guide me on how to utilize these values in the .ts file.

Thank you for your help!

Answer №1

It seems like you're trying to retrieve the value of a specific radio button in your .ts component file. If that's the case, this information should guide you in the right direction.

Learn how to get the checked value from a radio button in Angular

All you need to do is create a method or function that will be triggered when a radio button event occurs. I hope this clarifies things for you.

.html

 <ion-item *ngFor="let item of porsiyonlar">
     <ion-label>{{item.porsiyon}}
          <p>{{item.fiyat}} ₺</p>
     </ion-label>
     <ion-radio value="{{item.id}}" (change)="onItemChange($event)"></ion-radio>
 </ion-item>

.ts

onItemChange(value){
   console.log("The value is: ", value);
}

Answer №2

To retrieve the chosen item from the 'porsiyonlar' array, you can utilize the id and find method.

getPortionType() {
 const selectedItem = this.porsiyonlar.find(i => i.id === this.porsiyon);
 console.log(selectedItem.porsiyon,selectedItem.fiyat);
} 

Alternatively, you can attach a value directly to the object itself.

<ion-radio [value]="item"></ion-radio>

Component

getPortionType() {
 console.log(this.porsiyon.porsiyon, this.porsiyon.fiyat, this.porsiyon.id);
} 

Answer №3

 The code snippet provided filters through 'porsiyonlar' array to find a matching id with 'porsiyon'. Once found, it assigns the corresponding 'porsiyonadi' value. A successful implementation, thanks to everyone who contributed.

This solution is correct, my gratitude to all involved

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

Display an ellipsis (...) in ngx-datatable columns when the data exceeds the width

Seeking guidance on using ngx-datatable with Angular (). Currently facing an issue where text gets cut off when exceeding column width. Looking to implement ellipsis and show full details upon hovering over the truncated text or data. Any assistance ...

Enhance ngx-bootstrap's tab content with ngx-scrollbar functionality

Currently in my project, I am utilizing bootstrap 4 and ngx-bootstrap. One requirement I have is to develop a component consisting of 2 scrollable divs that can be switched by tabs. I was hoping to demonstrate a sample application on stackblitz, but unfor ...

Angular2 routing does not trigger the Component constructor and Router life-cycle hooks when the router.parent.navigate method is called from a child component

I am currently working on an application that includes child routes. The parent component (App component) consists of 2 routes: @RouteConfig([ { path: '/overview', name: 'Overview', component: OverviewComponent, useAsDefault:true }, { ...

angular directive to receive an object

When I have a table and click on a row, the information is supposed to be displayed in a different component using the @input decorator. However, instead of displaying the correct result in my other component, I am getting [object Object]. table.html < ...

"Is there a way to extract a value from a JSON object using

I have an object with the following key-value pairs: { 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': '918312asdasc812', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Login1&a ...

Error encountered in Typescript when handling fetch options as a variable

Why does this code compile perfectly? fetch('someurl', { method: 'GET', credentials:"same-origin" }) However, the following code throws a compilation error for fetch('someurl', init); const init = { method: &apo ...

Angular 4 application encountering 'Access-Control-Allow-Origin' issue

Attempting to reach the following URL: Manually accessing works without issue. However, trying to access via an Angular 4 request results in: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://loca ...

fetching data with Contentful and GatsbyJS

I am currently working on fetching data from Contentful using GraphQL within a Gatsby application, and here is my approach: type AllContentfulBlogs = { allContentfulBlogs: { nodes: Array<{ title?: string | null | undefined, ...

I need to find a replacement for punycode in npm as it has been marked as outdated. What should

In my Angular project, I have been utilizing the npm module punycode. However, I recently noticed that VsCode is indicating that this module is deprecated. Upon further investigation at https://nodejs.org/api/punycode.html#punycode_punycode, it has been co ...

Exploring Geographic Navigation with Angular Maps

Currently, I'm implementing Google Maps into my Angular SPA by following this helpful article. The HTML code in app.component.html looks like this: <html> <head> <meta charset="utf-8" /> <title>Map</titl ...

Row Model for Server-Side Processing

Looking to maintain the default loading overlay while serverSideRowModel functions are invoked in my Angular app, and prevent the loading spinner from showing up during data fetching. ...

Is there a way to verify if a value is undefined before including it as an object field?

I'm currently working on an Angular project and I have a query regarding TypeScript. It's about correctly handling the scenario where a field should not be included in an object if its value is undefined. In my code, I am initializing an object ...

It seems that there is an issue with Ionic v4 and Angular when trying to retrieve the inner text of ion-slide elements in loop

I have a set of statuses displayed in <ion-slide> using the *ngFor directive to loop through a specified array: status: string[] = [ 'Active', 'Inactive', 'Ended' ]; @ViewChild('slideWithNav') slides: Io ...

An asynchronous function operates incessantly

I have implemented the following code in React using hooks to fetch data from two different sources. const [ permissionTree, setPermissionTree ] = useState([]); const [ availablePermissionsInRole, setAvailablePermissionsInRole ] = useState<Permission[] ...

Mastering the art of mocking Rxjs Subject in an Angular application

I've set up a centralized DataStore to manage my report connections, handling events like onShow, onError, and onCancel so that the implementer doesn't need to worry about it. Now, I'm trying to figure out how to mock the SomeService.doSomet ...

Difficulty loading images in Angular 2 after deploying the dist folder (renamed to "myapp") to the tomcat webapp directory

When I pass the relative path and use the command below to build, I encounter a problem! ng build --prod --aot --base-href /myapp/ The issue arises with a 404 Resource not found error. bkgraph.jpeg:1 GET http://localhost:8081/assets/bkgraph.jpeg 404 () ...

Firebase initialization unsuccessful due to incorrect directory placement

I've been encountering an issue while deploying my Angular 2 project to Firebase. The initial deployment was successful, but subsequent attempts only show the Firebase Hosting welcome page instead of my project in the URL. I've realized that even ...

Encountering the error message "Unable to connect to this site" while attempting to run Angular 8 using Docker Compose

After successfully running npm start for my angular UI application, I encountered an issue when moving API and UI into docker. Every time I tried to access the site, it displayed "This site can’t be reached". Can someone please assist me in identifying w ...

Leveraging Inversify for implementing a Multiinject functionality for a class with both constant and injected parameters

In my Typescript code, I have an insuranceController class that takes a multi inject parameter: @injectable() export class InsuranceController implements IInsuranceController { private policies: IInsurancePolicy[]; constructor(@multiInject("IInsu ...

"Unexpected compatibility issues arise when using TypeScript with React, causing errors in props functionality

Just the other day, my TypeScript+React project was running smoothly. But now, without making any changes to the configurations, everything seems to be broken. Even rolling back to previous versions using Git or reinstalling packages with NPM does not solv ...