What is the best way to assign a series of radio buttons to an array within an Angular controller's model?

Let's say I have a controller that contains an array property named 'houses'. I want to use ng-repeat to display this array on a table row with a set of radio buttons (true/false, etc.). How can I ensure that selecting any of these radio buttons will update the corresponding item in the model array? I attempted to set the ng-model attribute on the radio buttons like so:

ng-model="array[{{$index}}]"

Unfortunately, this approach did not work as expected. Instead of getting the index value, I ended up with the literal string '$index'. Has anyone successfully achieved this functionality in Angular before?

Answer №1

To get it to work, you don't need the curly brackets. I have created a sample in Plunker.

$scope.houses = [true, false, true, false, false];
<ul>
  <li ng-repeat="house in houses">
    Checked: <input type="checkbox" ng-model="houses[$index]" />
  </li>
</ul>

{{houses}}

If you are dealing with a complex object and want to set its property (like

ng-model="houses[$index].checked"
), you should simply use the iterator (i.e. ng-model="house.checked"). This way, house will iterate over each of the array objects.

Answer №2

It has come to light that there is a known issue related to this specific scenario. Unfortunately, it appears that achieving the desired outcome may not be possible as the radio buttons operate within their own scope due to the presence of the ng-repeat.

However, an alternative solution involving the use of the checked attribute can be implemented: check out the code here

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

What is the process for redirecting to a different URL when the button is clicked?"

I am trying to display Home.js at localhost/posts, but when the button is clicked, it displays information at the auth.js URL. How can I link the button so that if the login information is correct, it redirects to "localhost/posts" instead of rendering its ...

"Revolutionary AJAX-enabled PHP social commenting system with multi-form support

Why is it that when I submit forms using these ajax functions in PHP, they only send to the first form on the page? I have multiple forms under each article and I want them to be submitted separately. What am I doing wrong? ...

Updating the title with respect to the current route in AngularJS 1

Is it possible to dynamically change the title displayed in a header based on the current route provided by the router, even when outside of the controller scope? For example, I have a mainMenu controller that is loaded when a specific route is called. The ...

Creating a custom JavaScript function to manipulate arrays

I am working on a project involving an array of strings that represent class names. My goal is to instantiate these classes using their string names, but so far my attempts with the window object have been unsuccessful. The array structure looks like this ...

Utilizing React and Google Code to Enhance Lead Conversion Pages

I have developed a basic react application featuring a contact form. Upon submission, I aim to display the Google Code for the lead Conversion Page within the application. <!-- Google Code for Purchase Conversion Page --> <script type="text ...

Resetting the check status in html can be accomplished by using the checked

I am currently working on a popup feature in HTML <div id="term_flags" class="term_flags"> <div class="modal-users-content flagsContent"> <div class="modal-users-header"> <span class="close" ng-clic ...

Troubleshoot AngularJS in Visual Studio (but not in Visual Studio Code)

Are there any methods for debugging AngularJS code within Visual Studio other than VS Code? I am specifically interested in setting breakpoints and analyzing the code. While I am aware that there are other IDE's that offer this functionality, I am cur ...

The Angular template in the attribute is not being interpreted correctly

How can I implement video rotation using Angular 1? <video width="400px" height="300px" autoplay loop> <source ng-attr-src="{{videos[currentVideo].url}}" type="video/mp4" /> </video> Even though the attribute is set properly, the vide ...

Optimal approach for reutilizing Javascript functions

After creating a simple quiz question using jQuery, involving show/hide, addClass, and tracking attempts, I am now considering how to replicate the same code for multiple questions. Would it be best practice to modify all variables in both HTML and jQuery, ...

Save the language code (ISO 639) as a numerical value

Currently, I'm working with a MongoDB database and have made the decision to store certain information as Numbers instead of Strings for what I thought would be efficiency reasons. For instance, I am storing countries based on the ISO 3166-1 numeric s ...

Tips for transferring client-side data to the server-side in Angular, Node.js, and Express

Seeking a straightforward solution to a seemingly basic question. I am utilizing Angular's $http method with a GET request for a promise from a specific URL (URL_OF_INTEREST). My server runs an express script server.js capable of handling GET reques ...

What is the best way to pass JavaScript object literals from a Node.js server to the frontend browser?

In Node, I am working with JavaScript object literals containing methods in the backend. For example: const report = { id: 1, title: 'Quarterly Report for Department 12345', abstract: 'This report shows the results of the sales ...

Having trouble connecting my chosen color from the color picker

Currently, I am working on an angularJS typescript application where I am trying to retrieve a color from a color picker. While I am successfully obtaining the value from the color picker, I am facing difficulty in binding this color as a background to my ...

Are you familiar with the Pagination and Card Components offered by Ant Design (antd)?

Can you merge the Pagination feature from antd with Card components to create a layout resembling Pinterest, complete with pagination? The standard Pagination code from https://ant.design/components/pagination/: import { Pagination } from 'antd&apo ...

Tips for updating the value of a $scope variable and running tests with AngularJS Jasmine

For my initial case, the product id should be undefined. var $stateParamsStub = { scheduleId : undefined } Within my controller, I am determining if isUpdate should be true or false. If the productId is undefined, then isUpdate should be false; other ...

Receive alerts in Swift from WKWebView when a particular screen is displayed in HTML

Check out this sample HTML file I have. I'm currently using the WKWebView to display this HTML. My goal is to receive a notification in our Swift code when the user finishes the game and the "high score" screen appears, so we can dismiss the view and ...

Different scenarios call for different techniques when it comes to matching text between special characters

I encounter different scenarios where strings are involved. The goal is to extract the text in between the || symbols. If there is only one ||, then the first part should be taken. For example: Useless information|| basic information|| advanced informa ...

Unable to reach the sub-component of the JSON object that was returned

For some reason, I can't seem to access a sub object of a returned $Resource object that fetched a group of JSON objects. It's baffling me. Resource > $resolved: true > $then: function (b, g) {var j=e(),h= > data: Object > 519bc5f6 ...

Having trouble retrieving complete object details through ng-model while utilizing ng-options

I am working with a property called cardDetails that contains an array of objects var cardDetails = [{ "cardHolderName":"XXXXXXXXXXXXXXXXXXXXXXXXXX", "cardNumber" :'1234' "mobileNumber":"91098765678", ...

Creating pathways in AJAX with Rails

Issue at hand: undefined variable article_id. The objective: Setting up the correct route for AJAX and Rails. What I require: The format articles/1/comments/2. Main goal: To specifically load comment through AJAX, not article. In the current AJAX scrip ...