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

What is the importance of using a server to host an Angular 2 app?

I recently finished developing a front-end application in Angular 2 using angular-cli. After completion, I created a bundle using the command ng build. Here's what puzzles me - my app consists only of JavaScript and HTML files. So why do I need to h ...

jQuery $.ajax problem encountered

When working with AngularJS, we have the ability to catch errors using the $http() service as shown below: return $http(defaultConfig).then(sendResponseData)**.catch(errorCallBack)**; On the other hand, when attempting to do something similar in jQuery: ...

AngularJS: Sort by

I have a list of names using ng-repeat. I would like the names to be displayed in alphabetical order. Here is the code snippet: <div ng-repeat="n in names | orderBy:'name'"></div> Anne Jane John Mike Ziggy However, I am ...

Global AngularJS service

I am attempting to save a response variable in a global service variable. Here is my service: (function() { angular.module('employeeApp') .service('constants', constants); function constants() { this.url = &apo ...

Dealing with the error "Type 'date[]' is not assignable to type '[date?, date?]' in a React hook

I'm attempting to assign a date range but encountering an error that states: Type 'Date[]' is not assignable to type '[Date?, Date?]'. Types of property 'length' are incompatible. Type 'number' is not assignab ...

Using TypeScript, what is the best way to call a public method within a wrapped Component?

Currently, I'm engaged in a React project that utilizes TypeScript. Within the project, there is an integration of the react-select component into another customized component. The custom wrapped component code is as follows: import * as React from " ...

Utilizing a function within the catchError method

A function has been defined to handle errors by opening a MatSnackBar to display the error message whenever a get request encounters an error. handleError(err: HttpErrorResponse) { this.snackBar.open(err.message) return throwError(err) } When subs ...

Ways to retrieve information beyond the scope of the 'then' method

One issue I am facing involves a piece of code that is only accessible inside of the 'then' function. I need to access it outside of this block in order to utilize it in other parts of my program. $scope.model = {'first_name':'&ap ...

Substitute all properties of a specific type with a predetermined value in Typescript using recursive substitution

If we consider the given type structure: type Person = { name: string age: number experience: { length: number title: string } } Can we create a type like this: type FieldsOfPerson = { name: true age: true experience: { length: t ...

Angular searchbar issue: Unable to assign parameter of type 'unknown' to parameter of type 'string'

I've been working on an Angular app to fetch data from an API successfully. However, I'm currently facing a challenge while implementing a search function in my component.ts file. Here is the entire service code: import { Injectable } from &apos ...

``I would like to discuss the use of TypeScript in returning a boolean value from

I am new to Angular and Typescript, and I need help returning a boolean value from a function that I can use in *ngIf. I want this process to be seamless. Can someone assist me with this? canView = false; getView() { this.permissionService.getPermissi ...

HTML Code for Tracking Facebook Friends' Likes on a Page

I am looking to show the number of friends who have liked a page in HTML. Can someone please guide me on this? I am aware that we can get the page likes using a simple URL, but I specifically want to know how to retrieve the count of friends who liked a ...

"Exciting developments in Angular 17 with the introduction of the new @

I need to output elements from an array of strings starting at index 1. arr = [ "str1", "str2", "str3", "str4", "str5" ] The desired output is: str2 str3 str4 str5 To achieve this, use a new @for loop in ...

Executing the filter function with specific parameters from a separate controller

Context: To improve performance issues caused by a large number of filtered objects, I decided to switch from using a filter inside my ng-repeat to manually calling the filter function. However, this change in architecture has led me to encounter the foll ...

What is the best way to conduct tests on this React component using Jest?

I'm currently working on increasing the test coverage for a wrapper component in my codebase using jest. Although I haven't been able to identify any specific usage of this component, it's important to ensure that it is covered by tests. M ...

Design a data structure that encompasses the combined output of multiple functions

I have a set of functions and I want to combine the return types of these functions into a union type. Example Input function types: type InputType1 = () => {type: "INCREASE"} type InputType2 = () => {type: "ADD", by: number} Ou ...

Using $route to obtain URL parameters

I am faced with the challenge of passing the last parameter from the following URL to a $http.get request in my Angular application. http://myurl.dev/users/32 However, I am struggling to figure out how to pass the 32 as the id. Here is what I have tried ...

merging JavaScript objects with complex conditions

I am attempting to combine data from two http requests into a single object based on specific conditions. Take a look at the following objects: vehicles: [ { vId: 1, color: 'green', passengers: [ { name: 'Joe', ag ...

Leveraging server-sent events to retrieve an array from a MySQL database using PHP

I've been attempting to implement server-sent events in order to fetch data from a database and dynamically update my HTML page whenever a new message is received. Here's the code I'm using: function update() { if(typeof(Event ...

Strategies for managing database modifications in a MEAN stack project

Online resources are scarce on this topic, but I am facing a common issue in application development. My project involves multiple users accessing and editing a shared database simultaneously. The challenge is ensuring that all users have an accurate and ...