AngularJS provides a way to create isolated scope for individual items within a list

I am using ng-repeat to populate a list of items.

<ul>
    <li ng-repeat="item in items" ng-class="setClass" ng-click="assignClass()">{{item.name}}</li>
</ul> 

In my controller, I have the following code:

$scope.assignClass = function(){
    $scope.setClass = "sampleClass";
}

The issue I'm facing is that when I click on any item, all items get the 'sampleClass' added.

My desired scenario is that when I click on the first item, only that item should have the 'sampleClass'. When I click on the second item, both the first and second items should have the 'sampleClass'.

How can I achieve this behavior?

Answer №1

The previous code you were using applied the styling to all list items. I have made a modification to the code so that each individual item in the unordered list can have its own class. You can now use the assignClass function with the selected item as a parameter to apply the desired class to that specific item.

<ul>
    <li ng-repeat="item in items" ng-class="item.setClass" ng-click="assignClass(item)">
        {{item.name}}
    </li>
</ul> 

$scope.assignClass = function(selectedItem){
    selectedItem.setClass = "sampleClass";
}

Answer №2

Invoke the assignClass function with the provided item.

<ul>
    <li ng-repeat="item in items" ng-click="assignClass(item)">{{item.name}}</li>
</ul> 

Subsequently adjust the class of that particular item.

$scope.assignClass = function(item){
    $(item).toggleClass("newSampleClass");
}

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

Creating an effect where an image gradually enlarges from its midpoint

A little while ago, I submitted a query about how to adjust the size of an image using the input type="range" feature. To my delight, user Prisoner provided me with a fantastic solution: <input id="ranger" type="range" min="1" max="100" value="100" /&g ...

Encountered a 404 error while utilizing the Java - Angular web service

Encountering an error 404 in Firebug when attempting to send an object from Angular to a Java controller using JSON. While the backend in Java is able to receive the message, Angular is unable to find the specified path. Consequently, there is an issue wit ...

What is the best way to ensure that Jest waits for an event to occur before performing an assertion?

I've developed a facade for the nats streaming lib in the following way: import nats, { Message, Stan, Subscription, SubscriptionOptions } from 'node-nats-streaming' class NatsHelper { private client: Stan | null = null public connect( ...

What is the method for altering the track color on a range slider?

Struggling to change the track color of a range slider using CSS in Chrome? Look no further, as I found a solution using jQuery (link). However, after implementing the solution, the expected output is not achieved. jQuery: $('.text-size-slider .slid ...

Activate Bootstrap 4 dropdown on mobile devices/responsive design

In my header, there are some links. As the header shrinks to fit a smaller screen, it switches to mobile view (collapse style). I'd like the dropdown menu to be visible by default, but still allow users to toggle it off and on as needed. In essence, ...

Transmitting user input to server using AngularJS

I have structured my HTML in the following way: <form ng-submit="addNews(news)" ng-controller='FormController'> <input type="text" placeholder="author" name="author" ng-model="todos.author"> <input type="submit" value="click"> ...

Only execute the NPM script if there is a staged JavaScript file

How can I ensure that an NPM script runs only when a JS file is staged, specifically after a pre-commit git hook (using Husky)? The scripts in my package.json are as follows: "scripts": { ... "test": "jest", "precommit": "npm test", ... }, ...

AngularJS app enhanced with personalized directive for drag and drop functionality

As part of my AngularJS application, I am implementing a feature where users can drag elements from the left container and drop them into the right container. While most elements can be dragged back to the left container, I am facing an issue with the "Man ...

Interpreting binary decimal data using JavaScript

The Challenge Received data from an API contains binary information about colored points in 3D space. The goal is to decode these points and utilize them in a buffergeometry within Three.js. Working with binary formatted data is proving to be beyond my c ...

To reveal the secret map location, just tap on the button - but remember, this only applies to

I am encountering an issue with duplicating a card and toggling the hidden location map. Currently, the location button only works for the first card and not for the rest. I need each card to independently open and hide the location map when clicked on the ...

Execute the script when the document is fully loaded

Is there a way to show a dialog in jQuery when the document loads without using <body onload="showdialog();">? Can the javascript code be placed in the main div or footer div to work like the onload event? <body onload="$('#dialog').sli ...

Encountering typeahead provider issues when attempting to upgrade UI Bootstrap version from 0.12 to 0.13

After upgrading to ui bootstrap 0.13 to take advantage of the popover template feature, I encountered an error that reads: Unknown provider: $templateRequestProvider <- $templateRequest <- tooltipTemplateTranscludeDirective Unknown provider: $templ ...

Where is the best place to use preventDefault in code?

I am currently working on setting up two select elements, prodSelect and calcSelect. The goal is to have calcSelect disabled if prodSelect has a value selected. I'm wondering if I placed preventDefault in the correct location within my code. I've ...

Unveiling and Shifting Among Concealed HTML Forms

After going through numerous tickets with similar questions, I still can't seem to achieve what I want. So, I have no choice but to ask this question myself. I currently have an HTML page with 2 forms and 2 buttons. Both forms are initially hidden us ...

Issue with bootstrap tooltip visibility within a specific div container

I am trying to align a div tag to the right, but when I add a Bootstrap tooltip to the left of it, the tooltip is not appearing. <div class="container-fluid"> <div class="row"> <div id="map" style="width: 100%; height ...

Creating a variety of NextJS components

Currently delving into NextJS and aiming to showcase a website featuring my various projects created with it. A special "Tag" component functions as a button template that allows custom text passed through props: export default function Tag({val}) { r ...

Why do two date type variables have identical content?

I'm trying to grasp why the value of d1 changes in each alert(). Any insights would be greatly appreciated. Thanks! <script> d1 = new Date("01/01/2015"); d2 = d1; alert(d1); d2.setDate(d2.getDate()+10); alert(d1); </script> ...

Create a function to produce a list of dates within two specified date ranges using JavaScript

Looking for assistance as a beginner in JavaScript. Can anyone guide me on how to generate a list of dates between dateA and dateB? For instance: dateA = 07/01/2013 dateB = 07/01/2014 Desired outcome: 07/01/2013, 07/02/2013, 07/03/2013, 07/04/2013...a ...

Concluding the dialogue once the post request has been successfully processed

My tech stack includes React, Redux, NodeJS, and ExpressJS. For the front-end, I'm utilizing material-ui. Within my application, I have implemented a dialog that allows users to input information and sign up. Upon clicking submit, a POST request is in ...

An uncaught error has arisen within Electron-forge: [object Object]

After attempting to create an electron based application by executing npm run make in the terminal, everything seemed to be going smoothly until it reached the stage of Making distributables. Although the output folder was successfully generated, the appli ...