AngularJS radio button slider with ng-model and ng-checked functionality

I'm facing an issue where my ng-model is not getting updated when I cycle through radio button images using arrows instead of clicking on the image. How can I resolve this?

HTML

<div ng-repeat="contact in contacts" 
     ng-show="showContactID == {{contact.contact_id}}">
    <div class="radio text-center">
        <label>
            <input type="radio" 
                   value="{{contact.contact_id}}" 
                   ng-checked="showContactID == {{contact.contact_id}}"
                   ng-model="transaction.contact_id">

            <img src="public/img/profiles/{{contact.contact_id}}.jpg" 
                 class="img-circle" 
                 height="150">
            <h4>{{contact.contact_name}}</h4>
        </label>
    </div>
</div>

<div class="row text-center">
    <div class="col-xs-6">
        <a class="btn" 
           ng-click="changeContact('prev')">
         <i class="fa fa-chevron-left fa-2x"></i></a>
    </div>
    <div class="col-xs-6">
        <a class="btn" 
           ng-click="changeContact('next')">
         <i class="fa fa-chevron-right fa-2x"></i></a>
    </div>
</div>

CSS

.transaction .form-group.contacts input {
    display: none;
}

.transaction .form-group.contacts .radio {
    padding-left: 0;
}

JavaScript

$scope.changeContact = function(which){

    var currentContactID = $scope.showContactID;
    var newContactID = 0;

    if(which){

        angular.forEach($scope.contacts, function(contact, key){

            if(contact.contact_id == currentContactID){

                if($scope.contacts[which == 'next' ? key + 1 : key - 1] == undefined){

                    newContactID = $scope.contacts[which == 'next' ? 0 : $scope.contacts.length - 1].contact_id;

                }
                else{

                    newContactID = $scope.contacts[which == 'next' ? key + 1 : key - 1].contact_id;

                }

            }

        });

        $scope.showContactID = newContactID;

    }

};

Answer №1

Every time the modifyContact function is invoked, be sure to also modify the value of transaction.customer_id with newCustomerID within the same function.

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

Guide on setting a wait while downloading a PDF file with protractor

As I begin the download of a pdf file, I realize that it will take more than 2 minutes to complete. In order to verify if the file has successfully downloaded or not, I will need to wait for the full 2 minutes before performing any verification checks. C ...

Having trouble receiving a callback after sending an AngularJS $http.post request

I am experiencing a problem with the success/fail handlers not being called when making an Angular $http.post request. The server side of my application is a .NET MVC controller, which successfully receives the file and functions correctly in that aspect. ...

Display the value of the dynamically added selected element only

Is there a way to display the selected element value on click without showing all element values? In the code below, when I add usernames and click on any label, it should only show the corresponding delete icon. However, currently, it shows delete icons f ...

Is there a reason why I can't load all Google charts on one webpage?

I am trying to use the Google Charts API to display various charts on my web page. However, I seem to be encountering an issue where the last chart is not loading correctly. Can someone point out what I might be missing in my code? I would greatly apprecia ...

Is a non-traditional fading effect possible with scrolling?

I have implemented a code snippet to adjust the opacity of an element based on scrolling: $(window).scroll(function () { this = myfadingdiv var height = $(this).height(); var offset = $(this).offset().top; var opacity = (height - homeTop + ...

Customize the appearance of alert boxes in ajax requests

Is there a way to customize the default ajax alert box? I currently have code that deletes a user and reloads the current page. It functions properly, but I want to enhance the styling of the alert box that appears before the user is deleted. function d ...

The JavaScript code is not being executed, as the address bar displays the function name instead

In my project, I have created numerous "js-classes" with various functions spread across different files. Unfortunately, the codebase is too large to share entirely. However, towards the end of the project, I encountered a bug where a specific function wa ...

Having trouble accessing JSON file again: "Encountered unexpected end of input error"

I have set up a cron-based scheduler to periodically retrieve JSON data from an external API every 2 minutes. The process involves writing the data to a file, reading it, cleaning it, and then storing it in a MongoDB collection. Everything works smoothly o ...

Nextjs - resolving the issue of shopping carts displaying incorrect total amounts

I am currently facing an issue with the total cart amount while building a shopping cart. The problem arises when I visit the cart page as it only displays the amount of the last item added to the cart. state = { cart: { items: [], total: 0 }, }; ad ...

Comparison of Ajax and submit: Why am I not receiving identical responses?

Utilizing Ajax for submission and all global variables ending in box. Note that the initialization code is functioning correctly. function begin() { if (confirm("Proceed at your own risk as all consequences are yours!!")) { var usernameNa ...

What is the best way to calculate the combined total of radio button values using jQuery or JavaScript?

I recently came across a tutorial on how to sum radio button values using JavaScript or jQuery, and I decided to implement it in my code. However, I encountered some issues as it didn't work as expected. My goal was to offer users the option to downlo ...

Implementing Autocomplete search with jQuery Chosen plugin - a step-by-step guide

CSS <select class="custom" id="company_hub" name="company_hub" style="width: 400px; display: none;"> <option>-Select Item-</option> </select> https://i.sstatic.net/x4YtN.png I attempted the following method but it was unsucces ...

Unable to generate a select element using the JSON data retrieved from an Ajax request

After making an AJAX call to fetch some JSON objects, I successfully retrieve them. However, I encountered an issue while trying to generate a select element from the returned JSON - it doesn't seem to create one. This is what my JavaScript looks lik ...

Creating an array object in JavaScript is a straightforward process that involves using the array

Looking to achieve the following object list structure: myObj = {"foo":[1,2,3,4], "bar":[3,5,7,8]} Initial attempt was unsuccessful: var myObj = new Object(); myObj["foo"].push(1) myObj["foo"].push(2) #...etc Seeking guidance on the correct m ...

Getting the width of children components in Reactjs can be achieved by using the

Check out this sample from the web: https://codesandbox.io/s/jovial-resonance-5mjq0 const Parent = styled.div` display: flex; width: 100%; background: yellow; justify-content: space-between; `; const Children = styled.div` display: flex; back ...

Disabling a button based on the result of a database query

Is there a way to dynamically enable or disable a button based on the result of a database query in JavaScript? I have managed to display an error message (with id="error") depending on the result, but toggling the button (with id="generate") doesn't ...

identifying the element targeted on dynamically created links with the help of jquery

I have created dynamic links where clicking on a country link displays the cities of that particular country. I am trying to retrieve the event.target.id of the dynamically generated city link when it is clicked. $(".countryName").children().on('cl ...

Having trouble extracting data from a particular cell within an HTML table using jQuery

I am struggling to extract row data from a table for utilization in a modal. Provided below is my code for Views. <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css"> <link href="@Url.Content(" ...

Is it possible to show or hide a DIV based on the text content of another DIV using JavaScript in

I am looking for a way to dynamically hide a DIV based on user roles using only the text inside a title tag as a reference. Here is an example of the HTML structure: <title>admin</title> If the user role is admin, then hide the following DI ...

As the value steadily grows, it continues to rise without interruption

Even though I thought it was a simple issue, I am still struggling to solve it. What I need is for the output value to increment continuously when I click the button. Here is the code snippet I have been working on: $('.submit').on('click&a ...