Looping through ng-repeats, extracting checked checkbox values in Angular

I am currently dealing with multiple nested ng-repeats in my project, and the third level down consists of a group of checkboxes. Initially, I receive an array of options for these checkboxes, which I handle with the following code snippet:

<div class="fadingOptionsHere" ng-repeat="fading in fade.options">
    <input type="checkbox" ng-model="fadingsHere" value="{{fading.id}}">{{fading.name}}
</div>

My goal is to only retrieve the values of the selected checkboxes. Ideally, I would like to replace the nested options array with an array containing only the selected items, allowing me to send back the JSON object in that format.

Since this is at the third level of nesting, I am facing difficulties in tracking these selections. How can I extract only the selected values (i.e., fading.id) from the checkboxes for each iteration of the ng-repeat loop?

So far, my attempts to reference the fadingsHere model have been unsuccessful.

Any assistance on this matter would be greatly appreciated!

Answer №1

One way to achieve this is by following these steps.

To implement this in HTML, use the code snippet below.

<ul>
    <li data-ng-repeat="record in records">
        <input type="checkbox" ng-model="selected[record.Id]"> {{record.Id}}
    </li>
</ul>

In your controller, make sure to include the selected property.

function MyCtrl($scope) {
 $scope.records = [ { "Id": 1, }, { "Id": 2 }, { "Id": 3 } ];
 $scope.selected = {};
 $scope.ShowSelected = function() {
    return $scope.selected 
};      

By accessing the selected property, you can retrieve the list of selected items.

Check out the demo here

Answer №2

One way to implement a checklist model directive is by using the following code snippet:

      <input type="checkbox" class="form-control"
                           checklist-model="transaction.jobData[attribute.key]"
                           checklist-value="checkBoxAttributes.code">

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

Incorporate an HTTP Header into a Wicket Ajax Request

I am trying to include a custom HTTP header in all Ajax (XHR) requests made by Wicket. I attempted the following: $.ajaxSetup({ beforeSend: function(xhr) { xhr.setRequestHeader('X-My-Header', 'value'); } }); and $(doc ...

Creating Browser Extensions with Vue.js and Vue CLI

I am in the process of creating a Chrome Extension with a frontend powered by Vue.js. Everything was going smoothly using vuecli until my app started utilizing the Webextension-API. This API is only accessible to registered Extensions, not normal websites. ...

Converting YAML Data From Database Into Array Within Rails Controller

I have a collection of integers stored in my database as yaml format. I am seeking a solution to convert the yaml data back into an array within my controller. Here is the database migration snippet: class AddRandomExerciseArrayToUsers < ActiveRecord: ...

Are the files selected by the user not displaying properly in the URL?

I'm currently working on a project using PhoneGap where I am facing an issue with uploading files and adding all file names to the parameters. The desired format is: http://www.example.com/uplaod.html?file=file1,file2,file3 To achieve this, I have a ...

JavaScript encounters an unexpected identifier: Syntax Error

I encountered a syntax error while executing the code below: var userAnswer = prompt("Do you want to race Bieber on stage?") if userAnswer = ("yes") { console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!") } else { ...

Bizarre String Behavior in jQuery JSON Through Ajax Request

Something unusual is occurring: whenever I try to send the string "??" to the server using AJAX $.ajax({ type: 'POST', url: path, dataType: 'json', data: JSON.stringify({ text: "??" }) }); it consistently results in send ...

Implementing dynamic loading with a Vue component loader

Is it possible to dynamically import a component using a default Loader if it's not loaded? Currently, we are using the following approach: import Dashboard from '../components/dashboard'; Vue.component('dashboard', Dashboard); ...

Tips for referencing a function declared within a prototype

I have been attempting to enhance a web page by adding functionality using a jquery library that lacks documentation. The problem I am encountering is primarily due to my lack of understanding of the jquery plugin model and/or the inner workings of javascr ...

Creating a Matrix-style display of 2D arrays using Python

If I possess a matrix within a numpy array in Python In [3]: my_matrix Out[3]: array([[ 2., 2., 2., 2., 2., 2., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 2., 2., 2., 2., 0., 0., ...

Tips for redirecting a page in React by forcing a route

Attempting to implement the guidance from this Stack Overflow solution on how to "go back home" after closing a Modal... import React, { Suspense, useState } from 'react'; import { BrowserRouter, Route, Switch, useHistory } from "react-route ...

Increase the identification of HTML element with jQuery

I've encountered an issue while trying to increment the id of 2 HTML elements, hwAddition and itemNumber, upon a button click event. The HTML code in question is as follows: <div id="hwAddition"> <div id="itemNumber" s ...

Unlocking the power of accessing nested data in JSON files dynamically

Building a new feature that allows users to input a word, choose the language, and receive the definition along with an example using the API service. To retrieve the desired data at position 0 of "exclamation" in the "meaning" section. This ensures that ...

Unable to assign a variable beyond the scope of a PHP foreach loop

Struggling to figure out how to make $leadsource print or echo correctly outside of the foreach statement. If I try to echo $leadsource outside of the foreach loop, it doesn't display the correct value from the array. It only works when I echo it ins ...

PHP displaying the contents of an array

A function is returning an array with the following type: Array ( [0] => stdClass Object ( [post_id] => 48 To display the contents of the array, I attempted to use the following foreach loop: foreach ($posts as $post){ echo $post['post_id ...

Learn how to import MarkerClusterer from the React Google Maps library without using the require method

The sample provided utilizes const { MarkerClusterer } = require("react-google-maps/lib/components/addons/MarkerClusterer");, however, I would prefer to use the import method. I attempted using: import { MarkerClusterer } from 'react-google-maps/lib ...

What is the best way to establish anchors for *ngFor elements in Angular 2 and beyond?

I have a component that displays items using *ngFor. My goal is to scroll down to the element with anchor #3. Here's the code snippet: @Component({ selector: 'my-app', template: ` <button (click)="scroll(3)">scroll 2</butt ...

What are some techniques for preventing Null Pointer Exception errors?

Currently, I am working on a project that involves creating a form with a dropdown field for categories. Based on the chosen category, I need to populate a second dropdown called subcaste using AJAX. To achieve this, I have implemented a method that disab ...

Has Apache initiated a forced logout process?

Running a LAMP server, I have implemented Apache basic authentication to log in users accessing the server homepage. I am currently seeking a way to enforce user logout, but my attempts with mod_session & mod_auth_form have not been successful. The page ...

Webpack does not support d3-tip in its current configuration

I'm having some trouble getting d3-tip to work with webpack while using TypeScript. Whenever I try to trigger mouseover events, I get an error saying "Uncaught TypeError: Cannot read property 'target' of null". This issue arises because th ...

What can be done to ensure that the a href tag is functioning as clickable?

Having an issue with my HTML and CSS code for a notification dropdown box. I am unable to click the tag, even after attempting to use JavaScript. Can't seem to figure out what's causing this problem. Any advice on how to make the tag clickable? ...