Troubleshooting ion-radio loop error in Ionic 2

I am encountering an issue with the ion-radio component in Ionic 2.

The problem is that when the component retrieves data from a service using HTTP and assigns it to the data property within the ngOnInit lifecycle hook, the radio buttons are not able to be selected.

Template:

<ion-list class="rooms" padding radio-group>
    <ion-item *ngFor="let r of data?.rooms">
       <ion-label >{{ r?.name }}</ion-label>
       <ion-radio [checked]="selectedRoom == r?.id" [value]="r?.id"></ion-radio>
    </ion-item>
</ion-list>

Component:

import { Component, OnInit } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';

import { BookConfirmPage } from '../bookconfirm/bookconfirm';
import { RoomDetailPage } from '../roomdetail/room';

import { HotelService } from '../../providers/hotel/hotelservice';

@Component({
  selector: 'page-hotel-detail',
  templateUrl: 'hotel.html',
  providers: [HotelService]
})
export class HotelDetailPage implements OnInit {

  public data;
  public selectedRoom: number;

  constructor(
    private navCtrl: NavController,
    private hotelServ: HotelService,
    private navParams: NavParams
    ) { }

  ngOnInit(): void {
    // You need an interface to make the data readable and consistent.
    // This method is not recommended for production. For learning purposes only.
    let id = this.navParams.get('id');
    let idRoom = this.navParams.get('idRoom');

    this.hotelServ.get(id).then(res => {
      this.data = res;
      //console.log(res);
    })
    .catch(err => console.log(err));

    this.selectedRoom = idRoom;
  }

  showBookConfirm(id) {
    console.log(this.selectedRoom)
    this.navCtrl.push(BookConfirmPage, { 'id': id });
  }

  showRoomDetail(id) {
    this.navCtrl.push(RoomDetailPage, { 'id': id });
  }

}

Edited: This is simply an interpolation issue in my code. Just change the template to use [value]="r?.id".

Answer №1

Simply make a modification

<ion-list class="rooms" padding radio-group>
  <ion-item *ngFor="let r of data?.rooms">
    <ion-label >{{ r?.name }}</ion-label>
    <ion-radio checked="{{ selectedRoom == r?.id }}" value="{{ r?.id }}"></ion-radio>
  </ion-item>
</ion-list>

to turn it into

<ion-list class="rooms" padding radio-group [(ngModel)]="selectedRoom">

  <ion-card class="room-card" *ngFor="let r of data?.rooms">
    <ion-card-content>
      <div class="left">
        <ion-radio [value]="r?.room_id"></ion-radio>
      </div>
      <div class="right" (click)="showRoomDetail(r?.room_id)">
        <ion-card-title>
          {{ r?.name }} {{ r?.room_id }}
        </ion-card-title>
        <p color="warning">&nbsp;Rp. {{ r?.cost }}</p>
      </div>
    </ion-card-content>
  </ion-card> 

</ion-list>

This problem stems from an interpolation issue within my code, rather than a flaw in Ionic.

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

A Guide to Catching Targeted React Notifications in Sentry using Next.js

In my Next.js application, I have implemented Sentry for error tracking. While I have successfully set up Sentry to capture errors within my try/catch blocks, I am currently struggling with capturing specific errors and warnings at a global level in my sen ...

The method JSON.stringify is not properly converting the entire object to a string

JSON.stringify(this.workout) is not properly stringifying the entire object. The workout variable is an instance of the Workout class, defined as follows: export class Workout { id: string; name: string; exercises: Exercise[]; routine: Ro ...

Accessing node_modules in TypeScript within an Asp.Net Core application

As I work on building a straightforward ASP.NET Core application utilizing npm and TypeScript, the structure of my project is organized as follows: / root | wwwroot | js | AutoGenerated // <-- TS output goes here | view | i ...

Issue with offline functionality in Angular 4 project

I am currently working on an E-commerce Project utilizing Angular 4, Bootstrap, and font-awesome. Additionally, I have incorporated .scss files, angular-cli, and have custom-built a slider component. All libraries, images, and necessary files have been ins ...

Creating regex to detect the presence of Donorbox EmbedForm in a web page

I am working on creating a Regex rule to validate if a value matches a Donorbox Embed Form. This validation is important to confirm that the user input codes are indeed from Donorbox. Here is an example of a Donorbox EmbedForm: <script src="https: ...

The issue persists with Angular's overflow hidden not functioning properly, and similarly, the show password button is

I created a login page using Angular Material, but I'm facing some issues. The problem is that when I set the style overflow: hidden, the show password button stops working. Additionally, if I click the button twice, the webpage refreshes, which shoul ...

Having difficulty customizing Mui Accordion with Styled Utility implementation

I am having trouble overriding the CSS for an Accordion using Mui styled utility. I am trying to apply a custom CSS class, but there seems to be an underlying class that is causing issues in my code. Here is the Mui class snippet: <div class="MuiPa ...

Minimize the gap between legend text and icon in Highchart

Is there a way to decrease the space between the number and icon? I am currently working with Angular 8 and Highchart. Below is the configuration of the chart legend. https://i.stack.imgur.com/0dL7y.jpg this.legend = { align: 'center', verti ...

Angular2 - Breaking down applications into reusable components

Utilizing custom properties permits seamless data binding between multiple components. <section id="main"> <app-home [dict]="dict">Hello there!</app-home> </section> In this scenario, dict serves ...

Error in custom TypeScript: Incorrect error instance detected within the component

I encountered a unique issue with my custom Error export class CustomError extends Error{ constructor(message: string) { super(message); Object.setPrototypeOf(this, CustomError.prototype); this.name = "CustomError"; } Furthermore ...

Disregarding TypeScript import errors within a monorepo ecosystem

In my Turborepo monorepo, I have a Next.js app package that imports various components from a shared package. This shared package is not compiled; it simply contains components imported directly by apps in the monorepo. The issue arises with the shared co ...

determining the properties of a given data type

I am currently working with a type that I have obtained from a third party source. My goal is to determine the type of a specific property within that type, using TypeScript. For example: type GivenType = { prop: string; } type desiredType = <&l ...

Ensuring a User has an Image in MySQL Using Angular 6

As part of my development process, I am working on creating a new user and sending their information along with an image to a MySQL database. The process involves sending a user object with form data through the following component.ts file: subscribeUser() ...

The collaboration feature in Angular is not functioning properly

In my application, I have two modules aside from the main module: - The Global.module, which contains shared modules; - The Pages.modules, which includes my pages. My goal is to make the modules in Global.module accessible to Pages.modules. However, I enc ...

Sharing functions between Angular components

Check out my problem statement: https://stackblitz.com/edit/angular-jk8dsj I'm facing two challenges with this assignment: I need to dynamically add elements in the app.component when clicking a button in the key-value.component. I've tried ...

AngularFirestoreCollection can be thought of as a reference to a collection within Firestore, which

Hey there, I need some help with the following code snippet. Data link service file private dbUser = '/users'; constructor(private firestore: AngularFirestore) { this.userCollection = firestore.collection(this.dbUser); } Now in my component fi ...

What is the process of accessing the changelog.md file within a component in Angular?

My challenge is to showcase the content from my changelog.md file, which is situated at the same level as the package.json file. I created a service for this purpose using the following code, function getData() { return http.get('../c ...

Conditional type/interface attribute typing

Below are the interfaces I am working with: interface Movie { id: number; title: string; } interface Show { title: string; ids: { trakt: number; imdb: string; tmdb?: number; }; } interface Props { data: Movie | Show; inCountdown ...

Angular jsonp.get request was denied despite receiving a status code of 200 indicating success

I have been attempting to access my basic web API created in Jersey, which returns the following data: [ { "id": 1, "name": "Facebook", "userName": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f4 ...

ngPrime table column selection and data extraction

I am looking to extract multiple columns from a table. Can anyone suggest the best approach for this? Does NGPrime offer any functionality for achieving this task? Appreciate your help! ...