"Ionic with Angular is facing an issue where ion-radio element cannot be set as checked by default

Having trouble selecting a radio button in a radio list. Can anyone help?

Any assistance would be greatly appreciated.

This is how I've been attempting it:

<div class="list">

      <ion-radio ng-repeat="item in goalTypeList"
                 ng-value="item.value"
                 ng-change="goalTypeChanged(item)"
                 ng-checked="item.selected"
                 ng-model="data.clientSide">
          {{ item.text }}
      </ion-radio>

  </div> 

JS:

.controller('SettingsCtrl', function($scope, $ionicLoading) {

        $scope.goalTypeList = [
            { text: "Dials", value: "dials", selected: true },
            { text: "Conversations", value: "conversations", selected: false },
            { text: "Appointments", value: "appointments", selected: false },
            { text: "Orders", value: "orders", selected: false }
        ];

        $scope.data = {
            clientSide: 'ng'
        };

        $scope.goalTypeChanged = function(item) {
            console.log("Selected goalType, text:", item.text, "value:", item.value);
        };

Answer №1

It appears that the value stored in data.clientSide does not match any of the values in the goalTypeList. Please update the value to align with one of the options listed below.

html:

<div class="list">

  <ion-radio ng-repeat="item in goalTypeList"
             ng-value="item.value"
             ng-change="goalTypeChanged(item)"
             ng-checked="item.selected"
             ng-model="data.clientSide">
      {{ item.text }}
  </ion-radio>

js:

  .controller('SettingsCtrl', function($scope, $ionicLoading) {

    $scope.goalTypeList = [
        { text: "Dials", value: "dials", selected: true },
        { text: "Conversations", value: "conversations" , selected: false  },
        { text: "Appointments", value: "appointments" , selected: false },
        { text: "Orders", value: "orders", selected: false  }
    ];

    $scope.data = {
        clientSide: 'appointments'
    };

    $scope.goalTypeChanged = function(item) {
        console.log("Selected goalType, text:", item.text, "value:", item.value);
    };

Answer №2

The value specified in

$scope.data = { clientSide: 'ng' };
does not correspond to any of the options available in $scope.goalTypeList.

If the value of clientSide in $scope.data is changed to either dials, conversations, appointments, or orders, then one of the radio buttons should be automatically selected.

I trust this explanation clears things up for you.

Answer №3

As mentioned by @Marc Harry, ensure that your ng-value matches the value of ng-model. For a more dynamic approach (for instance, if the selected value is retrieved from the backend and may change), you can implement the following:

<div class="list">

      <ion-radio ng-repeat="item in goalTypeList"
                 ng-value="item.value"
                 ng-checked="item.selected"
                 ng-model="data.clientSide">
          {{ item.text }}
      </ion-radio>

</div> 

.controller('SettingsCtrl', function($scope, $ionicLoading) {

    $scope.goalTypeList = [
        { text: "Dials", value: "dials", selected: true },
        { text: "Conversations", value: "conversations" , selected: false  },
        { text: "Appointments", value: "appointments" , selected: false },
        { text: "Orders", value: "orders", selected: false  }
    ];

    $scope.data = {
        clientSide: getSelectedValue()
    };

    function getSelectedValue() {
        for(let i = 0; i < $scope.goalTypeList.length; i++) {
            if ($scope.goalTypeList[i].selected) {
                return $scope.goalTypeList[i].value;
            }
        }
    };

Answer №4

Another option is to utilize ion-select along with the ionChange event:

 <ion-select (ionChange)="validateSelection($event)"  interface="popover" 
  placeholder="Make a Selection" >
    <ion-select-option *ngFor="let item of dataList" [value]="item"> 
   {{item.choice}}</ion-select-option>
</ion-select>

In your TypeScript file:

validateSelection(event){ console.log(event.detail.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

Changes made to the data are not reflected in the user interface, but they are visible in the console

When working on a React project with input fields, I encountered an issue where the date data can be changed but doesn't get reflected in the UI. Instead, the updated data is visible in the console. The code snippet below showcases how I'm using ...

Column reverse flex-direction is functioning correctly, however, it is imperative that it does not automatically scroll to the bottom

I have a Flexbox set up where I need the contents to display in reverse order. Everything is working well, but the list has many items and the newest ones are appearing at the top. Currently, the Flexbox automatically scrolls to the bottom, but I want it t ...

Navigating on Angular 2 without refreshing the page

Seeking advice for my Angular 2 single-page application project which heavily uses maps. The main requirement is that when a point on the map is clicked, the URL should update so that upon refresh, the map zooms into that specific point (and also to allow ...

Encountering the error message "RegistrationPO is not a constructor" while logging with Protractor

Whenever I run my spec file as shown below, I keep receiving the error message "RegistrationPO is not a constructor". Can someone please assist me in resolving this issue? Thank you in advance. //Registration_spec// 'use strict'; var FunLib = r ...

Contrasting {} and {} as any in TypeScript

Seeking clarity on TypeScript, what sets apart (foo and foo2) from (foo3 and foo4)? (foo and foo2) as well as (foo3 and foo4) produce identical results, yet during compilation, a red underline appears under foo2 and foo3. https://i.stack.imgur.com/lWaHc. ...

Trouble with AJAX BitMovin: unable to process GET request

I've been struggling to find a solution to my problem as I cannot pinpoint what exactly to search for. My current issue involves using AJAX for a PHP Get request, causing it to submit and navigate away from the page. The BitMovin library appears to b ...

What is the best way to create a summary module that consolidates and re-exports all the exported functionalities from multiple sub-modules in E

Is there a way to re-export the exports from multiple files in an ESM module without manually listing each export? I am looking to convert my CommonJS module directory, which contains several files, to ESM imports/exports. Currently, I have an index.js fi ...

Using Google Maps: How can I trigger an InfoWindow to open by hovering over a link?

Trying to figure out how to make the "info window" on my map pop up when hovering over a corresponding link in the list of points. Currently, you have to click on a point to see the information. Any suggestions on how to achieve this while ensuring that al ...

Verification of unique custom string

How can I ensure that a string follows the specific format of x.xx.xxxxx? The first character is mandatory, followed by a period, then two characters, another period, and finally any number of characters of varying lengths. ...

Struggling to get JQuery timing events to function properly?

Encountering timing issues with the events in my self-made simon memory game. Experimented with setTimeout but struggling to figure out which events to time and the appropriate duration for them to be distinguishable. $('#play').click(function( ...

BufferGeometry is not being displayed

After working extensively with BufferGeometry, I considered myself quite familiar with it. However, I am currently facing an issue while trying to create a simple square plane - there is no visible plane, no errors, and everything seems to be set up correc ...

What could be causing the AntiForgeryToken validation to fail while implementing AngularJS in an ASP.NET MVC application?

I am encountering a problem with passing the correct AntiForgeryToken when using AngularJS $http service in an ASP.NET MVC application. Here are the solutions I have attempted: Setting HTTP Request headers with an httpInterceptor app.factory('ht ...

Utilizing the reduce method to process an object and return a collection of objects

I have a complex object that I'm trying to transform using the reduce method, but I'm struggling to figure it out... Here is the structure of my object: const object = { ... } My goal is to create a new object with keys that are a combinatio ...

MQTT Broker specialized in Typescript

I'm looking to create a MQTT Broker using TypeScript and Angular. I've attempted a few examples, but I keep encountering the following error: Uncaught TypeError: http.createServer is not a function Here's a simple example of what I'm c ...

Modify the style of a webpage through JavaScript

Need help with calling a JS event based on button presses and changing CSS font styling accordingly for each button? Check out the code snippet below: body { background-image: url("back2.jpg"); background-size: 100% 100%; } ...

Refresh needed for Material UI styles to load correctly in Next JS

UPDATE: Reproducible issue https://github.com/ganavol409/next-material-ui-classes-bug Issue seems to be related to Higher Order Components and importing useStyles from Material UI Implemented Solution: https://github.com/mui-org/material-ui/blob/master/ ...

What is the best way to utilize AJAX in sending chosen files to a PHP script?

I currently have two forms set up. Form A includes fields for name, age, address, email, and a hidden text field that holds the names of images to be uploaded in form B. Form B allows users to browse and select their photos using an input type File. ...

What is the best way to add content in JavaScript?

Just diving into the world of JavaScript, HTML, and web development tools here. var labels = {{ labels|tojson|safe }}; After using console.log to check the content of labels with console.log(JSON.stringify(labels));, I got this output: [ {"id":"1", ...

Error: Docker/Next.js cannot locate module '@mui/x-date-pickers/AdapterDateFns' or its respective type definitions

When I run the command npm run build, my Next.js application builds successfully without any issues. However, when I try to build it in my Dockerfile, I encounter the following problem: #0 12.18 Type error: Cannot find module '@mui/x-date-pickers/Ada ...

Trouble retrieving data (React-redux)

Greetings! I am currently working on a project involving the Spotify API and attempting to retrieve new releases. While the data is successfully passed down to the reducer, I encounter an issue when calling the fetch action in my App component. When I try ...