Discover the method to retrieve the chosen value from a list of radio buttons using AngularJS

After making an AJAX request and receiving JSON data, I have created a list of radio buttons with values from the response. Although I have successfully bound the values to the list, I am facing difficulty in retrieving the selected value. Below is a snippet of my attempted code:

HTML

<ons-list-item modifier="tappable" ng-repeat="area in AreaList">
      <label class="checkbox checkbox--list-item">
          <input type="radio" ng-bind="area.name"  ng-model="selectedValue" name="area" value="area.code">
        <div class="checkbox__checkmark checkbox--list-item__checkmark"></div>
        {{area.name}}
      </label>
    </ons-list-item>

JavaScript

// Parsing for Area
if(data.Details[i] === "Area"){
    for(var d=0;d<17;d++){
        var area = data.Details[i+1][d]['varCityName'];
        var code = data.Details[i+1][d]['intGlCode'];

        $scope.AreaList.push({name:area, code:code});

        area = '';
        code = '';

        $scope.selectedValue =  data.Details[i+1][d]['intGlCode'];
    }

    console.log("===========AREA obj===========",  $scope.AreaList);
}

$scope.submitAnswer=function(list,rate,areaobj){
    gList = list.name;
    alert("-----------my selected area is=========="+gArea);
};

Printed Area Object in Console (long)

[
    Object{
        name="Camana Bay",
        code="32"
    },
    Object{
        name="Breakers",
        code="1"
    },
    Object{
        name="Grand Cayman",
        code="2"
    },
    Object{
        name="Bodden Town",
        code="16"
    },
    Object{
        name="Cayman Brac",
        code="15"
    },
    Object{
        name="East End",
        code="20"
    },
    Object{
        name="George Town",
        code="21"
    },
    Object{
        name="George Town West",
        code="22"
    },
    Object{
        name="Little Cayman",
        code="23"
    }
   ]

Answer №1

Consider this:

<input type="radio" ng-bind="category.name"  ng-model="selectedCategory" name="category" ng-value="category.code">

ng-model is used to define the variable that will store the selected value.

ng-value specifies the value that will be saved in ng-model if a specific radio button is chosen.

Take a look at this jsbin

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

Why does my JSON variable contain "type" and "data" values instead of something else?

After using JSON.stringify() on my object to save it to a file, I noticed that one of the parameters does not have the expected string value assigned. Instead, it has a "type" and "data". Code: fs.writeFileSync('myjson.json', JSON.stringify(myjs ...

JQuery is unable to initiate a keyup event

I am currently utilizing jQuery in a web application. On one of my pages, I have set up an event listener for keypresses as shown below: document.addEventListener('keyup', function (event) { event.preventDefault(); var key = event.k ...

Is it best practice to initialize loaded scripts before the JQuery .load callback is called or should it be done after the .ready callback in the loaded script?

When I click a button in my main document, I load the content of a specific div using jQuery: $("#my_div").load(function() { alert("Content Loaded"); }); The loaded content contains a script: <script> alert("Initial Alert from External Script" ...

Fetching the name of a JSON object in JavaScript converted to a string

Is there a way to retrieve the name 'muxEnvironments' as a string from the object? I need it in order to analyze and compare the content type. When I use console.log(obj), the entire object is displayed. My objective is something like this: jso ...

How can you locate the position of identified text on a webpage to accurately place the mouse cursor there?

When browsing a webpage in my web browser (preferably Firefox), I have the ability to search for a specific text "abc" using ctrl+f. Once found, I need to move my mouse cursor to another relative position on the page and click. Unfortunately, the necessar ...

How can you pre-load SVG images in an Ionic view?

After developing a mobile app using Ionic, I encountered a slow loading time for one specific view that includes a large SVG image of 202KB. The delay in loading the view/page can be frustrating as it takes around 3-4 seconds to fully load and display. Is ...

Effortless method to handle package.json configurations

Is there a better approach for seamlessly transitioning between using npm link and git, or another solution that caters well to both front end and back end developers? The dilemma I'm facing revolves around developing a website that utilizes multiple ...

Is there a way to modify the browser's user agent using JavaScript or HTML?

I currently have an application that is running on IE7. However, an analytics tool I rely on has recently been updated and now requires a minimum of IE8. Is there a way to modify the User Agent in IE7 using JavaScript or HTML to trick it into being recogni ...

The latest changes in the MongoDB route and model may result in either data deletion or an error being thrown

I've been searching for quite some time now, and I haven't found a similar issue to the one I am facing. Working on a MERN stack application with four routes, three are functioning properly. However, the fourth route and the database model are c ...

Generate an image instantly from text using ajax when a key is pressed

Currently, I am immersed in a stencil project where my goal is to convert text into an image. In this particular task, there's a textbox that captures the user input on key up event. Once the user enters text, my aim is to display that text as an imag ...

Ways to monitor the status of a server request using jQuery (ajax)?

I am currently working on a PHP application that involves users submitting forms to the server via ajax (jQuery). The server needs to insert a large number of records into a MySQL database, which may take some time due to various calculations. My question ...

Fill the rows of the <Table> using HTML code

I am encountering an issue with displaying data in a table on a screen: <table id="TransactionTable" class="table table-responsive table-striped"> <thead> <tr> <th></th> <th>Date</ ...

The ViewChild from NgbModalModule in @ng-bootstrap/ng-bootstrap for Angular 6 is causing the modal to return as

I have successfully integrated ng bootstrap into my project, specifically utilizing the modal module to display a contact form. The form includes input fields for email and message, as well as a submit button. You can find the ngbootstrap module I am using ...

Tips for preloading a script in nextjs

I'm having trouble incorporating a script into my website. The issue is that the script doesn't load properly the first time the page loads, but after a few refreshes, it works and the responsible iFrame shows up. I've attempted several di ...

Is there a way to streamline and optimize this React/Material UI code for faster performance?

There seems to be a lot of repetition in the code that needs to be cleaned up. I'm wondering if the switch statement is necessary. It looks like it requires the muiTheme palette to be passed this way. Also, can these theme constants be placed in a sep ...

Is Your CanvasJS Chart Traveling in Reverse?

My charts are displaying dates in reverse order, can anyone help me figure out what's causing this issue? I've checked the documentation but couldn't find anything that would explain this problem. Link to documentation: Here is a screensh ...

JavaScript first, middle, and last names

When working with Javascript, I have encountered a challenge. I am attempting to extract the First, Middle, and Last names from a full name input into three separate fields - Character Length, Middle Name, and Initials. At this point, I have successfull ...

Exploring Keypress events with Dojo's 'on' module

Recently, I've started utilizing Dojo's latest on module for event handling. It has been working well so far, but a new issue has cropped up. Specifically, when using the keypress event, I am unable to retrieve the character value (such as "2" or ...

Is there a way for me to receive numerical values instead of NaN?

I'm currently facing a challenge in creating a Fibonacci number generator and I've hit a roadblock. It seems like I have a solution, but the appearance of NaN's is causing me some trouble. function fibonacciGenerator (n) { var output = [ ...

Move a Java application from a trial SAP HCP to a complete SAP HCP membership

After successfully creating a Java IoT App with Raspberry Pi running on SAP HANA HCP trial account, I am now looking to enhance its functionality within the SAP HANA Development Workbench. Is there a way to import it into the SAP HANA account with ease, o ...