To ensure that checkboxes are automatically checked in AngularJS 1.5, the model should be bound to the objects without using the ng-checked

Due to various conflicts and dependencies, I have opted to use AngularJS 1.5.x instead of 1.6.x, which has led me to encounter some issues with ng-checked functionality.

Within my ng-repeat loop, I am dealing with 2 objects:

  1. vm.stateList, containing all 50 states
  2. vm.pager, a JSON object where selected states are listed

My goal is to display all states as checkboxes and have them pre-checked if they exist in the vm.pager object.

 div ng-repeat="states in vm.statesList"

Here is the code snippet for the checkbox that should be checked if the state is found in my object:

<input ng-model="vm.pager.location[$index].state" type="checkbox" id="state{{states}}">{{states.name}}

Below is an example of the data structure, highlighting the presence of the state in the location:

{
  "Id": 105,
  "Name": "blah",
  "Description": "other data",
  "$$hashKey": "object:98",
  "location": [
    {
      "Id": 96,
      "state": "NY",
      "Order": 9,
    }
  ]
}

Answer №1

To handle multiple checkboxes in AngularJS, you can create a function in your controller that is triggered by each checkbox:

<input ng-model="vm.pager.location[$index].state"
  type="checkbox" id="state{{states}}"
  checked="vm.isInState(states)">{{states.name}}

function isInState(state) {
  return vm.pager.findIndex((item) => item.location[0].state === state) > 0 ? true : false;
}

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

Discovering the import path of Node modules in ReactAlgorithm for determining the import path of

Software Development In my current project, I am utilizing Typescript along with React. To enhance the application, I integrated react-bootstrap-date-picker by executing yarn install react-bootstrap-date-picker. Unfortunately, there is no clear instruct ...

Using AngularJS controller to implement filtering functionality

I am a beginner at using Angular and have successfully implemented a filter to translate text for localization in my HTML view: <input type="button" class="btn btn-link" value="{{'weeklyOrdersPage.reposting' | translate}}" ng-click="sortBy(&a ...

In mvc.net 4, Ajax is only compatible with the httpGet method and not with httpPost

There are two methods, httpGet and httpPost, inside the Login action. Currently, when an ajax call is made, it only works with the httpGet method of Login. I would like it to work with the httpPost method of Login. Thank you in advance for your answer! ...

Creating a banner using four grid elements, with each element containing four child elements, is a simple process that can

Can you assist me in recreating my idea from an image? I am having trouble understanding how to select and place other elements, and then return them to their original positions after deselecting... I attempted to use a grid but it did not work properly. ...

The JavaScript function is designed to only accept whole numbers as input

Whenever I invoke a JavaScript function and pass along 4 parameters, it only functions properly if the fourth parameter consists of integers exclusively. Below is the code snippet for my onchange function: onchange="weekchange(this, <?php echo $i ?> ...

I am looking to manage the error handling service, and my next step is to intentionally insert a single error into my service and have it automated

Need help with error handling in my service. I want to manually insert a single error, but would like it to be done automatically instead. 'use strict'; angular.module('nexoolApp.errorservice', ['nexoolApp.config']) .servic ...

Tips for ensuring only one property is present in a Typescript interface

Consider the React component interface below: export interface MyInterface { name: string; isEasy?: boolean; isMedium?: boolean; isHard?: boolean; } This component must accept only one property from isEasy, isMedium, or isHard For example: <M ...

Empty the localStorage when terminating the IE process using the Task Manager

Utilizing HTML5 localStorage to keep track of my application session has been a useful feature. Here is a snippet of the code I am currently using: if(typeof(Storage)!=="undefined") { if(sessionStorage.lastname=="Smith") { alert("Your ses ...

Vue.js - Maintaining input value when model declines updates

I am working on a text input that allows users to enter numbers with a maximum of three digits after the decimal point: <v-text-field type="text" :value="num" @change="changeNum($event)" /> <p>{{ num }}</p> ... export default { data: ...

Issue: Child Pages not found in Nuxt RoutingDescription: When navigating

Currently working on a Nuxt application that utilizes the WordPress REST API to fetch content. While my other routes are functioning correctly, I am facing challenges with nested pages. The structure I have implemented in my Nuxt app is as follows: pages ...

The AngularJS change event is not being activated

I am a beginner with angular js and I have implemented a bootstrap calendar in my application. However, I am facing an issue where the change event is not being triggered when the month changes, no matter where I place it within the code. Here is the snip ...

Ways to transfer information from the server to the user interface in a WordPress extension

I am currently developing a WordPress plugin and have successfully created a form in the admin panel. Now, I am looking to transfer the data collected from that form to my Frontend JavaScript file. Is there a way to achieve this, and if so, what steps shou ...

What's the best way to showcase data in a column dynamically depending on its specific columns?

I have an array of objects representing columns and another array representing table data. I need to dynamically display the data in the specified columns. Some objects have 3 key-values which should be displayed in specific columns. The second object in ...

Troubleshooting problem with Karma and Jasmine when testing AngularJS and jQuery draggable components

I have an angular application that requires a small amount of jQuery for advanced UI drag and drop functions. I am utilizing jQuery UI draggables for this purpose. While everything else in my controllers is successfully tested using karma/jasmine, I am fa ...

"Seeking assistance with concept sharing and content transmission. In need of guidance

Basic Question I'm stuck trying to brainstorm a concept to reach my objective. I am having trouble figuring out how to navigate the DOM correctly with my chosen method. The Objective First, let me explain what I am doing: I use ajax to bring HTML ...

Is an input field/text area necessary for the traditional copy to clipboard feature?

I needed to copy a section of text enclosed within an anchor tag to the clipboard. Following suggestions found online, I implemented the code below: HTML: <div class="organizer-link"> <i class="fa fa-link" id="copylink"></i> <a href ...

Manipulating Strings in JavaScript

Hi there, I'm a beginner in JavaScript and I could really use some help with the following question. So, let's say I have this string: "AB_CD.1.23.3-609.7.8.EF_HI.XBXB" The numbers 1.23.3 and 609.7.8 are completely random with two dots separat ...

Difficulty in displaying JavaScript function output as text

I'm currently developing a program that randomly selects and prints a function from an array list. I am facing difficulties in getting the result to print correctly. Below is the snippet of code: const hiddenElements = document.querySelectorAll( &qu ...

What is the best way to utilize a JavaScript for loop to translate ASCII code into a word?

I am attempting to solve this code in order to determine the name associated with the array below: let numbers=[42,67,74,62,6A,1F,58,64,60,66,64,71]; for(let i=0;i<numbers.length;i++) { numbers[i] = numbers[i]+1; } Any guesses on what the resultin ...

A selection dropdown within a grid layout

Hey there, I am currently working on implementing a dropdown list within a data grid. I want to ensure that if I select a value in one row of the dropdown and then select the same value in another row, a message will be displayed saying 'already added ...