Modify the filtered class of ng-repeat using the $index value from the controller

Here is my ng-repeat code:

<div class="{{event.who_seen_it | newEvent}}" ng-repeat="event in eventList" ng-click="openEvet(event.title,$index)">
    <h3 class="event-title">{{event.title}}</h3>
</div>

I am using the {{event.who_seen_it | newEvent}} to apply a class to each repeated element. I would like to modify the output of this filter newEvent, add a new class to the current element, or remove the filter {{event.who_seen_it | newEvent}} by clicking on each repeated element.

I want to accomplish this using the $index from the controller. How can I achieve this?

Answer №1

To easily add or remove classes, utilize the power of ng-class. By creating a map with key:value pairs where the key represents the class and the value is a condition that, when met, adds the specified class to the element. For example, if we have a class called visited:

<ul>
  <li ng-repeat="event in ctrl.eventList track by $index" 
      ng-class="{'visited': event.visited}"
      ng-click="event.visited = true">{{event.title}}</li>
</ul>

Upon clicking an event, the visited property is set to true, triggering the addition of the visited class to the <li> element.

The ng-class directive allows for multiple key/value pairs with different conditions to be defined within an object/map structure. Explore this concept further in this example on Plunker.

For more information on how to effectively use ng-class, refer to the official ng-class documentation as there are various ways to leverage its capabilities.

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

Ways to verify that window.open is being invoked from a React component

In my React component, I have a set of nested components, each with its own checkbox. The state hook rulesToDownload starts as an empty array and dynamically adds or removes IDs based on checkbox selection. Upon clicking the 'download' button, t ...

change the css back to its original state when a key is pressed

Is there a way to retrieve the original CSS of an element that changes on hover without rewriting it all? Below is my code: $(document).keydown(function(e) { if (e.keyCode == 27) { $(NBSmegamenu).css('display', 'none');} ...

Utilize Webpack to integrate redux-form as an external library

I currently have a range of imports in my project, such as: import {Field, reduxForm, FormSection, formValueSelector} from 'redux-form'; My goal is to treat the redux-form imports as an external library so that they do not get included in the b ...

The componentWillUnmount method is not being called

I'm currently working on a Backbone application and I'm in the process of integrating React components. The React component is being mounted using the following code: ReactDOM.render( <WrappedComponent />, node ); where "node" represents ...

Error: Undefined object while trying to access 'injection' property

After updating to React 16.x, I encountered the following error: Uncaught TypeError: Cannot read property 'injection' of undefined at injectTapEventPlugin (injectTapEventPlugin.js:23) at eval (index.js:53) at Object.<anonymous> ...

Animating Chart.js 2 from right to left instead of from top to bottom

Here is the issue demonstrated in the jsfiddle below. Initially, the data inserts work well. However, when the data set reaches a cap of 10, an unwanted behavior occurs where the data points are animated top-down instead of moving leftward. This can be qu ...

Is there a way to link a particular model table with a designated user table?

Hey everyone, I'm new to stack overflow and this is my first question. I hope it's clear enough for you all to understand. I'm currently working on a budget API using Node.js, Sequelize, Express, and PostgreSQL. The API allows users to add/ ...

What could be causing the ng-options to not fill up this dropdown?

Here is my JSON data: [{"year":"1980","Manufacturer":"Oldsmobile","Model":"Cutlass","Product":""},{"year":"1980","Manufacturer":"Oldsmobile","Model":"Sierra","Product":""},{"year":"1980","Manufacturer":"Toyota","Model":"4Runner","Product":""},{"year":"198 ...

Uncover the solution to eliminating webpack warnings associated with incorporating the winston logger by utilizing the ContextReplacementPlugin

When running webpack on a project that includes the winston package, several warnings are generated. This is because webpack automatically includes non-javascript files due to a lazy-loading mechanism in a dependency called logform. The issue arises when ...

Textures have been added by the user in the three.js platform

Click here to access the jsFiddle adaptation of this problem. In my quest to develop a cutting-edge 3D web application, I aim to allow users to choose an image file from their device: <input id="userImage" type="file"/> Once a file is selected, th ...

Is it possible for JavaScript to interface with MySQL databases?

Is it possible to establish a connection between JavaScript and MySQL? If yes, what is the process? ...

Change the page using JavaScript after submission

I am currently working on incorporating Firebase authentication into a Node.js Express-based application. However, I have encountered the following error: nexttick.js:45 Uncaught TypeError: Cannot read property 'redirect' of undefined This ...

Developing Reactive Selections with Material-UI is made easy

After spending about 5 hours on a task that I thought would only take 15 minutes, I still can't figure out the solution. The issue is with my React App where I have two dropdowns in the Diagnosis Component for users to select Make and Model of their a ...

Tips for reusing multiple sequences of chained transitions in D3

Looking for a more efficient way to code two lengthy sequences of chained transitions with varying order, properties, and attributes. For example, one sequence might be a, b, c, d, e, f, g, h, and the other e, f, g, h, a, b, c, d. Tried the code below with ...

What is the process for attaching an on-click event listener by utilizing an argument that is passed to a function in JavaScript?

These are the functions I have created: function type(message) { document.getElementById("body").innerHTML="<div id=\"textbox\" class=\"textbox\"></div><div id=\"oc\&q ...

What is the best way to prevent a draggable div from moving past the edge of the viewport?

My situation involves a draggable div that functions as a popup window when a specific button is clicked. However, I noticed that dragging the div to the end of the viewport allows me to drag it out of the visible area, causing the body of the page to expa ...

What is the process for executing Selenium IDE scripts in Selenium RC?

As a newcomer to using the selenium testing tool, I am seeking guidance on running selenium IDE scripts within selenium RC. I would greatly appreciate examples and screenshots to help me better understand the process. ...

Is there a way to selectively include a filter in ng-repeat within a directive?

Utilizing an element directive across multiple views, the directive iterates through each 'resource' in a list of resources using ng-repeat="resource in resources". Different page controllers determine which resources are fetched from the API by ...

Tips for creating a dynamic menu item that stands out with a highlighted style

I would like to add a colored square to highlight the active menu item. .main-menu ul { padding: 0; margin: 0; text-align: center; } .main-menu li { list-style-type: none; display: inline-block; padding: 40px 0; } .main-menu a { font-fam ...

Having trouble accessing a CSV file with JQuery and FileContentResult?

How can I make an ajax call without using ActionLink in my controller? Here is a snippet of my controller code: public IActionResult ExportUsers(List<string> listOfEmails) { /*some data processing*/ return File(result, "text/csv", ...