I'm having trouble getting the conditional ngClass to function properly in my specific scenario

<li ng-class="{selected: 'checked==true'}" ng-repeat="item in data">
<span>item.name</span>
<input ng-model="checked"/>
</li>

Is there a way to dynamically add the 'selected' class only after clicking on the checkbox rather than having it initially added?

Answer №1

As stated in the question, you mentioned that your input is a checkbox. If there's a typo when declaring the input, it will be treated as a text input instead.

To ensure the correct input type, use the following code:

<input type="checkbox" ng-model="checked"/>

When using ng-model with checkboxes, the associated variable like checked in this case, will only hold values of true or false, which are boolean. Therefore, there's no need to compare them within the ng-class directive.

Just use

<li ng-class="{selected: checked}" ng-repeat="item in data">
and it will work properly.

If you want to see an example, check out the plunker I made for you here.

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

Can the lexical scope of a function in JS be maintained while overriding it?

When faced with the task of modifying a function provided by a third-party module, I considered simply copying the function and making changes to it. However, the issue lies in the fact that this function relies on other functions and variables within its ...

Validating a set of input fields in AngularJS for Form Verification

When working with Angular, I often utilize specific directives to validate individual input fields such as email or username. For my current project, I am faced with the following scenario: I need to establish an Advance Payment field, which must fall wi ...

In search of a jQuery parser for HTML strings that can accurately parse <a> links and <img> images

Is there a HTML string jQuery parser available, specifically for parsing <a> links and <img> images? Alternatively, I am looking for code that can extract all links and images from a HTML string, which can potentially be very large. For exampl ...

What is the method for concatenating two strings in JavaScript without any whitespace in between?

When working with two strings involving time, consider the following scenario: var gettime= $("#select-choice-2 :selected").text(); The above code returns a time value in 24-hour format, such as 17:45 However, if you require the time to display in the ...

Creating a Node API that can patiently listen for external data

My current project involves building a server that fetches data from an external API and returns it to the endpoint localhost:3000/v1/api/. However, I'm facing a challenge where the data retrieval process takes approximately 2 seconds, leading to empt ...

Troubleshooting Issues with Passing Values in jQuery AJAX POST Requests

Currently, I am working on two basic PHP pages: notification.php <html> <head><title></title> <meta charset="UTF-8"> <script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script> <script src="ht ...

creating sleek animations with Pixi.js for circular shapes

Is it possible to create smooth animations on circles or sprites similar to D3.js hits in Leaflet? https://drive.google.com/file/d/10d5L_zR-MyQf1H9CLDg1wKcvnPQd5mvW/view?usp=sharing While D3 works well with circles, the browser freezes. I am new to Pixi. ...

Combine the values in the array with the input

I received some data from the back-end which is being written to a form, and it's in the form of an array of objects Below is the code snippet: this.companyDetailsForm = new FormGroup({ directors : new FormControl(response?.companyDirectors) ...

myObject loop not functioning properly in Internet Explorer version 10

Could someone please point out what is wrong with this code snippet? HTML: <div id="res"></div> Javascript: var myObject = { "a" : { src : "someimagepath_a.png" }, "b" : { src : "someimagepath_b.png" }, }; va ...

Using services in Angular 6 CLI with dynamically added components

One of my recent projects involved creating a directive in Angular 6 called 'DeleteDirective' and integrating it with a service called 'DeleteService' to enable the deletion of items from the application. Once an item is deleted (handle ...

What's the best way to add line numbers to source code on an HTML webpage after parsing?

I am currently working with AngularJS and MongoDB. In my MongoDB data, there are some text elements that contain a \n, causing each line to be displayed on a new line based on the occurrence of \n. However, I also want to add line numbers to each ...

When I prevent 3rd party cookies, the Google Signin onsuccess function remains inactive

I've been working on an AngularJS application that utilizes the basic Google Signin process outlined here: https://developers.google.com/identity/sign-in/web/. Everything is functioning properly, and I'm able to sign in as a Google user. However ...

How can I allow scrolling within a specific div element?

Here is the structure of my layout: <html> <head> stuff goes here </head> <body> <div id="master"> <div id="toolbar"> <input type="text" id="foo"> </div> <div id="c ...

Using ng-options with distinct values does not function as expected

I have an array of cars that I want to use ng-options on to display only the color category as a link, along with an "all colors" option. All, red, yellow, blue <div ng-repeat="client in clients"> <label>{{client.Name}}</label> < ...

Retrieve Gridview properties using JavaScript

I need to adjust the font size of my gridview using JavaScript to make it more suitable for printing. What is the best way to change the font size specifically for a gridview using JavaScript? ...

The second pop-up modal fails to appear

I have successfully implemented 2 modal windows with the help of bootstrap. The first modal is used for adding a user to the database, and the second one is meant for editing an existing user. While the first modal works perfectly fine, the editing modal d ...

What are the best practices for creating and displaying functional components effectively?

I'm in the process of building and displaying a functional component following the guidelines provided as a starting point. After reviewing the instructions, it seems like I should be able to achieve something similar to this: class MyComponent exten ...

Having trouble with changing text in a link with an onclick event?

When I click on the link, I want the text to change to the second span. However, this functionality is not working. Code var reload = false; $('#change').click(function() { reload = !reload; $('#change').click(function() { ...

Currently in development is an exciting MMORPG created with Node.js, Express, and HTML. Players will have the ability to move exclusively to

I'm developing a multiplayer online role-playing game (MMORPG) using Node.js, Express, and HTML. Currently, I'm facing an issue with keyboard interactivity in my game. When I load the game, only the 'D' key seems to work, allowing the p ...

Rendering Vue components synchronously as a singular string

There exists a Vue SFC called Bubble, which contains a simple layout structure. Bubble.vue <script setup lang="ts"> </script> <template> <div hinted-name="wrapper-bubble" class="hinted-bubble-wrapper" ...