Unable to access ng-model values within ng-repeat loop

I'm having trouble setting ng-model values within an ng-repeat statement.

When I repeat this div with an array of JSON objects, I am able to print the 'ID' value of the object in any element. However, I can't use that as the ng-model value for the checkbox inside. I must be making a mistake somewhere. Any suggestions on what that might be?

I would greatly appreciate it if someone could take a look and offer some guidance.

For reference, here is a link to a codepen showcasing the issue: Code pen link

.

Answer №1

When assigning a value to a checkbox model, it is typically boolean, representing true or false, unless specified otherwise. This allows for only two options.

Instead of using 'id' as the model attribute, consider using another attribute that can store a boolean value. For example, why not use 'isSelected'?

<div ng-controller="quoteController" ng-app="MyApp" class="benefits-container">
    <!-- benefits -->
      <div class="benefit" ng-class="{'selected': pe.id}" ng-repeat="pe in policyEnhancementsArr | filter: {type:'optional'}">
          <div class="top">
              <md-checkbox ng-model="pe.isSelected" class="blue"></md-checkbox>
              <h5 class="item">{{pe.name}}</h5>
              <h5 class="prize">{{pe.loading}}</h5>
          </div>
          <div class="bottom">
              <p>{{pe.limitDisplay}}</p>
          </div>
      </div>
  </div>

Then update the 'isSelected' value:

...
{
  "id": "PVC022",
  "name": "NCD Protector",
  "limit": null,
  "limitDisplay": "N/A",
  "desc": "<TBC>",
  "type": "optional",
  "loading": 0.0,
  "isSelected": true
},
...

Answer №2

I just completed writing code that is very similar, except for the filter you used with ng-bind. I recommend checking out the article where they suggest utilizing ng-value.

Exploring the distinctions between ng-model and ng-value

Answer №3

Try implementing ng-repeat and ng-model in the same line for a more efficient code structure.

Here is an example of what you can do:

<div class="benefit" ng-class="{'selected': pe.id}" ng-repeat="pe in policyEnhancementsArr | filter: {type:'optional'}">
          <div class="top">
              <md-checkbox ng-model="pe.id" class="blue"></md-checkbox>

Instead of the above, try this approach:

<div class="benefit" ng-class="{'selected': pe.id}" ng-repeat="pe in policyEnhancementsArr | filter: {type:'optional'}" 
ng-model="pe.id">
              <div class="top">
                  <md-checkbox  class="blue"></md-checkbox>

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

Using ng-class-odd for Parent Elements in AngularJS

<div ng-repeat="object in data"> <!-- parent --> <section id="{{object.tag}}" ng-class-odd="'content text-center light'" ng-class-even="'content text-center dark'"> ... <!-- child --> ...

The innerHTML of the p tag remains unaffected when using document.getElementsById("")

HTML {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'lessons/style.css' %}" /> <script> function openNav() { document.getElementById("mySidenav").style.width = "20%"; document.getElementById("main" ...

Struggling to Manage and Preserve Cookies in Browser While Using React App Hosted on GitHub Pages and Node.js Backend Running on Render

I've encountered a challenge with setting and storing cookies in the browser while utilizing a React application deployed on GitHub Pages and a Node.js backend hosted on Render. Let me explain the setup and the issue I'm facing: Setup: Frontend ...

The HTML textarea is not updating properly when using jQuery's keypress event

I am facing an issue with my forms on a webpage, each containing an html textarea: <textarea name="Comment" class="inputTextArea">Hello World!</textarea> There is a javascript event handler that automatically submits the fo ...

AngularJS: Error - Angular object is undefined

Hello! I am currently working on a project to develop a simple App using c# WebAPI and AngularJS. Unfortunately, I have encountered an error in the console which is preventing the web app from functioning properly. Here is a snippet of my Index.html file: ...

What is the best way to retrieve a list of unchecked checkboxes in a Razor Page model?

Currently, I am working on a razor page using .NET Core 7. I have encountered an issue where I am unable to pass values of 1, 2, and 3 for unchecked checkboxes from the HTML page to the page model in the post method. When the user clicks the submit button ...

Comprehend the angular and in a confined scope and its brackets

I have been wondering why I have to use double brackets ()() when passing a method from an outer scope to a directive with an isolated scope using '&'. It seems like the function is returned with callThat() and then I need to call it with anothe ...

How can the @blur event be added to the vue-bootstrap-typeahead in Nuxt/Vue application?

I am facing an issue where I want to trigger a function upon leaving an input field. The input in question is set up using vue-bootstrap-typeahead. Upon inspecting the DOM, I found that the structure of the input element looks like this: <div id="m ...

Adjust the color of a contenteditable div once the value matches

I currently have a table with some contenteditable divs: <div contenteditable="true" class="change"> This particular JavaScript code is responsible for changing the color of these divs based on their content when the page loads. However, I am now ...

Mastering the art of switching between different application statuses in ReactJS

As a newcomer to reactJS, I am exploring ways to make one of my components cycle through various CRUD states (creating object, listing objects, updating objects, deleting objects). Each state will correspond to displaying the appropriate form... I have co ...

The declaration file for module 'react/jsx-runtime' could not be located

While using material-ui in a react project with a typescript template, everything is functioning well. However, I have encountered an issue where multiple lines of code are showing red lines as the code renders. The error message being displayed is: Coul ...

Make sure to trigger a callback function once the radio button or checkbox is selected using jQuery, JavaScript, or Angular

I'm looking to receive a callback once the click event has finished in the original function. <input type="radio" onchange="changefun()" /> function changefun() { // some code will be done here } on another page $(document).on('input: ...

Locate the parent and child elements within an array

Within the array provided below, there are parent items as well as children. I am currently able to identify parents (depth 0) and their immediate children (depth 1), however I am unsure how to handle deeper nested levels. For reference, you can view the ...

My React setup is causing some problems that I need to address

React is not my strong suit, but I need to test a React application. The issue arises when attempting to run the server using gulp nodemon, resulting in numerous errors. It seems that the application is built on an outdated version of React and some libra ...

Tips for utilizing Selenium Webdriver to input text into a webpage textbox using onblur, onfocus, and onkeydown attributes

I'm looking to streamline the process of inputting a value into a text box on a webpage, similar to automating my timesheet. The HTML code for the text box is shown below. <input type="text" class="" onblur="return setValue(this);" title="Time"; ...

Unable to execute an Angular 2 application within Visual Studio 2015

I encountered an error while trying to set up an environment on VS 2015 with Angular2. Whenever I run the command "npm start," I receive the following error message. I attempted using "npm cache clean --force" before running "npm start," but the error pers ...

Retrieve an array of information from a firestore database query

I am encountering an issue while trying to retrieve information about all users who are friends of a specific user. I have attempted to gather the data by pushing it to an array and then returning it as a single JSON array. However, it seems that the dat ...

Error! The function worker.recognize(...).progress is throwing an error. Any ideas on how to resolve this

Here is the code snippet: //Imports const express = require('express'); const app = express(); const fs = require("fs"); const multer = require('multer'); const { createWorker } = require("tesseract.js"); co ...

Mastering the A-Frame Game Loop: Tips for Separating Logic and Rendering

Currently, I am experimenting with A-Frame and my Quest 2 headset in order to create a simple VR game. One particular challenge I am facing is understanding how to separate logic from rendering and establish a proper game loop. After discovering this tutor ...

How to style a div for printing using CSS

Currently, I am working on a project that has already been completed but now requires some enhancements. To give you an overview, the project includes a search functionality that displays additional details upon clicking on the displayed name in the result ...