Ensure that selecting one checkbox does not automatically select the entire group of checkboxes

Here is the code I'm using to populate a list of checkboxes.

<label class="checkbox-inline" ng-repeat="item in vm.ItemList track by item.id">
<input type="checkbox" name="item_{{item.id}}" ng-value="{{item.id}}" ng-model="vm.selectedItem" /> {{item.name}}
</label>

The code above currently selects all items in the ItemList. However, I only need to select one item from the list. How can I achieve this?

Answer №1

Check out this helpful link: https://vitalets.github.io/checklist-model/

I found success using this resource!

1) Replace ng-model with checklist-model
2) Use checklist-value instead of ng-value

Answer №2

By linking the identical external variable vm.selectedItem to each checkbox, you inadvertently cause all checkboxes to be selected when any one is checked. This occurs because they are all bound to the same variable that has now been set to true.

To address this issue, I recommend assigning a separate selected property to every item in the ItemList. Subsequently, connect this property to the ng-model as illustrated below.

<label class="checkbox-inline" ng-repeat="item in vm.ItemList track by item.id">
     <input type="checkbox" name="item" ng-value="item.id" ng-model="item.selected" />{{item.name}}
</label>

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

Launching URLs from JSON data in the system browser - Apache Cordova

I am facing an issue with opening links from a JSON in the system browser on both Android and iOS devices. The link generated by the function appears as: {{item.addressLink}}, instead of bit.ly/xyzxyz Here is what I currently have: <a href="#" ng-cl ...

Asynchronous jQuery operations using promises and finally functionality

I am attempting to interact with a REST api using jQuery's ajax feature. My goal is to return the Promise<Customer> object, but I am encountering an error stating that the property finally is missing. It used to work before, so I assume there h ...

Executing MongoDB collection operations with array filtering

I am looking to count records based on tags and filter them before including in specific groups // data in database {tags: ['video', 'Alex'], ... }, {tags: ['video', 'John'], ... }, {tags: ['video', 'J ...

Angular's Restangular initiates a blank promise at the beginning

One of the services I created can fetch data from the server and cache it, or retrieve data directly from the cache. The code snippet below illustrates this functionality. While using .then statements in the client side for cached data, I've noticed ...

Looking up routes on Google Maps - Response from the Directions service

Using the Google Maps API to retrieve a route between two points. The response from Directions Service is used to render directions in HTML: $scope.directionsDisplay = new maps.DirectionsRenderer(); var directionsService = new maps.DirectionsService(); ...

best method for implementing a TypeScript object

I've been searching high and low, but I can't seem to find the exact answer I need. Please inform me if my question has already been asked. My goal is to construct an object in TypeScript that includes attributes of custom types. The issue I&ap ...

I am trying to figure out how to dynamically set the deployUrl during runtime in Angular

When working with Angular, the definition of "webpack_public_path" or "webpack_require.p" for a project can be done in multiple ways: By setting the deployUrl in the .angular-cli.json file By adding --deployUrl "some/path" to the "ng build" command line ...

retrieving data from a different controller in AngularJS

Having an issue with passing data from rootScope.reslogin2 to scope.user. It's not displaying as expected, here is my JavaScript file: app.controller("logincont", ['$scope','$http','md5','$window','$rootS ...

AngularJs operates similarly to the way JQuery functions

Hello everyone, I am hoping to achieve a similar functionality in Angular like this: $('[property="something"]').remove(). Could anyone assist me with this? ...

What should be included in the types field of package.json for TypeScript libraries?

I'm finding it challenging to efficiently develop multiple typescript modules simultaneously with code navigation while ensuring the correct publishing method. What should I include in the "types" field of my package.json? Referring to: Typescriptlan ...

Struggling to use the bind method for the loadScene callback function in cocosCreator or cocos2d-x?

When the loadScene() callback function is bound, the information retrieved from getScene() becomes inaccurate. Upon transitioning from the Entry Scene to the Lobby Scene, I perform post-processing tasks. The implementation was done in TypeScript. Entry. ...

Attempt to resend requests that exceed the time limit by utilizing the angular $resource and implementing the retry

Is it possible to retry a failed request within the failure callback without using an interceptor? The request failed due to a timeout or another reason. $resource(url, {}, {get: {method: "GET"}}).get() .$promise.then(function (r ...

Learn how to break down Angular 2 with Typescript in just 5 minutes by troubleshooting issues

I've been delving into the world of TypeScript and Angular 2 by following the guide at https://angular.io/guide/quickstart. After going through all the steps, I encountered some errors with the final step npm start. Here's what I got: Microsoft ...

Update the input value with the selected option from the dropdown menu in Angular

How can I dynamically set the value of an input field based on the selection from a dropdown menu in Angular using Reactive Forms? Below is my HTML code: <nb-card> <nb-card-header> Services </nb-card-header> <nb-card-body&g ...

Simulated service integrated into the Angular module initialization block

While working in the module.run block, I encountered a situation where I needed to call a method from a service that I had created. To properly test my controller without making actual http requests, I wanted to use a mock service instead of the real one. ...

Is it possible for the controller of a modal window to have access to functions within the parent controller

If you were to launch a modal window using $modal.open from an angular directive, would the modal window be able to access functions defined within the parent directive? Below is the code for the directive controller: function parentFunction() { re ...

Display the active item in ng-repeat using conditional ui-view

Here is the situation: I'm working with a list of items that are displayed using ng-repeat. Each item has its own template. When a user clicks on an item, that particular item is marked as "active" and gets a different template compared to the rest ...

MongoDB NextJS connection issue "tried to retrieve a connection from a closed connection pool"

I am attempting to establish a connection to my MongoDB database in order to retrieve some information. When setting up the connection without fetching any data, everything works fine. However, when trying to fetch data, the console throws this error: at ...

Utilizing the React TypeScript useContext hook with useReducer for state management

I'm struggling to identify the type error present in this code snippet import React from 'react'; interface IMenu { items: { title: string, active: boolean, label: string }[] } type Action = | { type: 'SET_ACTIVE&a ...

Embed the div within images of varying widths

I need help positioning a div in the bottom right corner of all images, regardless of their width. The issue I am facing is that when an image takes up 100% width, the div ends up in the center. How can I ensure the div stays in the lower right corner eve ...