There are multiple sets of radio buttons within nested ng-repeats, but only the final group displays the selected value

I am having an issue with updating a form that contains multiple radio buttons based on data retrieved from an API. The challenge is that only the last set of radio buttons displays the value correctly. Below is the code snippet I am using (angular brackets denoted by double square brackets - [[ ]]):

<fieldset data-ng-repeat="s in sections">
    <div class="form-group">
        <div class="col-md-12">
            <h2>[[ s.section.name ]]</h2>
        </div>
    </div>

    <!-- Field Item -->
    <div class="form-group m-b-20 bg-light" data-ng-repeat="f in s.fields">
        <div class="col-md-12 m-b-30">
            <h4>[[ f.field.name ]]</h2>
            <input type="text" data-ng-model="f.comments" class="form-control input-md underline" placeholder="Comments">
        </div>

        <div class="col-sm-3">
            <input type="radio" name="section-[[s.section.section_id]]-field-[[f.field.field_id]]" value="pass" class="form-control" data-ng-model="f.field_condition">
            <label class="eval-pass"><i class="fa fa-check-circle green"></i> Pass</label>
        </div>
        <div class="col-sm-3">
            <input type="radio" name="section-[[s.section.section_id]]-field-[[f.field.field_id]]" value="fail" class="form-control" data-ng-model="f.field_condition">
            <label class="eval-fail"> <i class="fa fa-exclamation-circle red"></i> Fail</label>
        </div>
        <div class="col-sm-3">
            <input type="radio" name="section-[[s.section.section_id]]-field-[[f.field.field_id]]" value="n/a" class="form-control" data-ng-model="f.field_condition">
            <label class="eval-na"> <i class="fa fa-circle blue"></i> N/A</label>
        </div>
        <div class="col-sm-3">
            <input type="radio" name="section-[[s.section.section_id]]-field-[[f.field.field_id]]" value="caution" class="form-control" data-ng-model="f.field_condition">
            <label class="eval-caution"><i class="fa fa-exclamation-triangle yellow"></i> Caution</label>
        </div>
    </div>
    [[ f.field_condition ]]
    <hr>
</fieldset>

The issue lies in the fact that only the last field in each section displays the selected radio button, while the others remain unselected. The data model seems to be properly set for each field, and manual selection also changes the model accordingly. However, the initial display doesn't reflect this. Interestingly, even if the selections are not visible initially, saving the form still updates the database correctly without setting values to null.

Could this possibly be influenced by CSS styling? The provided HTML and CSS files include custom styling for the radio buttons.

UPDATE

Despite recreating the app using the same code included in the working fiddle link, the issue persists where only the last group of radio buttons in each section displays the selected value. It remains unclear why this discrepancy exists, as the same code functions as expected in the fiddle example. Your insights on this matter would be greatly appreciated.

If you wish to examine the complete code of my new angular app to replicate the issue, it can be found here: https://pastebin.com/qSR33yfM

I have structured the app on a single page for simplicity. Additionally, the exact json data used in the application can be accessed via this pastebin link: https://pastebin.com/utfVVQfT

Answer №1

To solve the issue you're facing, I resolved it by incorporating an array of objects called $scope.values to represent the radio button choices. By utilizing ng-repeat, I was able to generate the radio buttons accordingly. Here is the revised code: https://pastebin.com/s3hNzaXX

I am aware of the nuances surrounding ng-repeat in creating new scopes and suspect a conflict with nested ng-repeats causing incorrect binding of radio buttons at a different scope level than intended.

To validate this assumption, you can convert all your interpolations in the code to use functions and utilize console.log statements with s and f at various points to verify if field_condition is set at an unintended level.

In any case, adopting the best practice of creating radio buttons through data (using ng-repeat as shown with the $scope.values array) not only enables seamless updating of value options via data sources such as AJAX but also helps avoid angular scoping challenges encountered in your current code snippet above.

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

The image is loaded correctly through the image picker, however, it is not displaying on the screen

When I click the button to pick an image from the gallery in this code, it is supposed to load the phone's gallery and display the selected image in the image component. Even though the image gets loaded properly (confirmed through test logs), it does ...

Navigate to the specified URL once the Ajax function has completed successfully

I'm having trouble with opening a URL from an Ajax function. It seems like the URL is not being called. This is the code I am using: $(document).on( "click",".btndriver", function() { var id = $(this).attr("id"); var nombre = $(this).att ...

Issue with Vue: Calling method instantly when using :mouseover in v-for within a component

Whenever I try to incorporate :mouseover in a v-for loop within the <component>, I encounter an issue. The method assigned to the :mouseover event is triggered for each element, rather than just for the hovered elements. <Single-technology ...

The initial render of Keen-slider in NextJS can sometimes encounter difficulties when using server-side rendering

While incorporating the keen-slider library v5.4.0 into my nextJS project with TypeScript, I encountered a peculiar issue. The error arises when the keen-slider is initially loaded for the first time, resulting in mismatched transform types causing items ...

Troubleshoot: Why is my Google Chrome not playing videos? Uncover the solution with a

I have created a webpage with an HTML video tag that loads dynamically from a JSON file. I am currently using Chrome 50 for this project. The response is successful, and when inspecting the page using Chrome's developer tools, I can see the video tag ...

Receiving a 404 error after refreshing the browser in Angularjs routing

I am currently working on a project that involves Angular JS and asp.net web api. Whenever I navigate using this URL http://localhost:3458/Profile An error pops up saying: Description: HTTP 404. The resource you are looking for... However, if I use th ...

Fulfill the promise in AngularJS and assign it to a different factory

Presenting my factory below: .factory('UserData', ['User', '$q', function(User, $q) { var deferred = $q.defer(); return { user: null, get: function() { var _this = this; _this. ...

Ensuring User Information Persistence in React Upon Successful Login

I am developing a small application using React with PHP as the back-end. Within my database, I have two types of users - admin and student. Upon user login, I store their information in session storage like this ( user: { username:'abcxyz123', r ...

How can I make a method in VueJS wait to execute until after rendering?

There is a button that triggers the parse method. parse: function() { this.json.data = getDataFromAPI(); applyColor(); }, applyColor: function() { for (var i=0; i<this.json.data.length; i++) { var doc = document.getElementById(t ...

Enhancing Communication Between JavaScript and PHP

Positioned within my form is an input field where users can enter their postcode. The shipping cost for their order will be determined based on this postcode, utilizing PHP to assign different costs depending on the zone. After the user enters their postc ...

AngularJS: Utilizing ng-click in ui-select choices

Trying to incorporate an add button within the options, but facing a challenge where ng-click triggers without selecting the option. <ui-select ng-model="datactrl.newservice.selected" theme="selectize" ng-disabled="disabled" style="width: 100%;"> ...

Problem with Pinia: nested array in an object

My unique store located within a vue3 application, contains an object called currentReservation with a property named pricings which is an array. Each pricing includes an id and quantity. When I add a new pricing, it is added to both the store and compone ...

An issue has emerged: React cannot render objects as children. The culprit appears to be an object containing keys such as {

My sanity schema, named blogs, includes a reference field called author. I am trying to use blog.author in order to fetch the author's name for my blog, but I keep encountering an error. https://i.stack.imgur.com/haotL.png The code in my Sanity blo ...

How to Troubleshoot jQuery AJAX Not Sending JSON Data

I've been attempting to make an ajax request, but it keeps returning with an error response. $('form#contactForm button.submit').click(function () { var contactName = $('#contactForm #contactName').val(); ...

Trouble with the filter function in the component array

I am facing an issue with creating and deleting multiple components. I have successfully created the components, but for some reason, I am unable to delete them when I click on the "delete" button. state = { data: '', todoCard: [], id ...

What steps can I take to resolve the CLIENT_MISSING_INTENTS issue?

After diving into learning about discord.js, I've run into a bit of a roadblock. Despite trying to troubleshoot by searching online, I can't seem to resolve the issue. const Discord = require('discord.js'); // const Discord = require(&a ...

Tips for combining HTML and JavaScript on a WordPress site

As a WordPress developer who is still learning the ropes, I have come across a challenge with embedding html and JavaScript onto a page. Currently, I am in the process of redesigning a company website and one of the tasks involves integrating a calculator ...

Manipulating the visibility of components by toggling on click events according to the existing state in ReactJS

Disclosure: I am still getting familiar with ReactJS I'm currently working on incorporating a dialog window for selecting a country/language on the initial page of my application. Here's the concept: There's a small flag button in the to ...

Dealing with undefined Ajax values in PHP

Every time I call the function, I receive an undefined value and it's baffling me as to what could be causing this issue. You are logged in as undefined, Error undefined Ajax script: function Submit() { console.log('asd') $.ajax({ ...

What is the method for receiving socket emits in sails.io.js?

I am currently utilizing Sails 0.11 for the back-end and angularjs for the front-end of my project. Within Sails, I have a TwitterController containing the following code snippet to establish a connection with the Twitter Streaming API using the node modu ...