Utilizing ng-class in AngularJS based on the specific html elements

I am displaying each item from my array as an HTML paragraph using the ng-repeat directive.

<p ng-repeat="item in queries track by $index">{{ item }}</p>

The output looks like this:

------------ title 1 --------------
content 1
------------ title 2 --------------
content 2
------------ title 3 --------------
content 3
------------ title 4 --------------
content 4

However, I'm struggling to find a way to apply custom styles to these paragraphs (e.g. color:red;).

Answer №1

When adding the ng-class attribute:

<p  ng-repeat="item in queries track by $index" 
              ng-class={someCustomClass: item.title} >{{ item }}</p>

If the above code doesn't work, you can try this instead:

<p  ng-repeat="item in queries track by $index" 
            ng-class={someCustomClass: Boolean(item.title) } >{{ item }}</p>

The key concept is to provide an object literal to ng-class, where the key represents the class name and the value indicates the condition for applying the class.

For example,

ng-class={ theClassNameToUse: TheCondition }

Answer №2

To employ ng-class, you can follow this format:

ng-class="{'class-name': item.indexOf('title') >= 0}"

Alternatively, utilize ng-style in this manner:

ng-style="setColor(item)"

Utilize the following function:

$scope.setColor = function (item) {
    if (item.indexOf("title") >= 0) {
        return { color: red };
    }
}

Answer №3

Here is a potential solution:

<div ng-repeat="element in data track by $index"
    ng-attr-style="{{ element.indexOf('value') !== -1 ? 'background-color: yellow' : '' }}">{{ element }}</div>

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

Identify the specific element using a designated data attribute

Having trouble figuring out how to select a specific element with a particular data attribute. In my situation, I want to target the a element with class="zoom-image" and data-order-item-id="<?=$order_item_id ?>". Here is the HTML/PHP code snippet ( ...

What is the solution for handling undefined errors that occur when employing d3.select(this) within a Vue methods hook?

Currently, I am in the process of transferring d3 graphs from another project to my personal Vue-based project. Most aspects are functioning as expected, except for aligning labels in the arcs of a pie chart using the arc.centroid(d) method. Two errors kee ...

How can I apply a texture to a 3D rectangle in THREE.js?

I am attempting to create a 3D box in THREE.js that represents a box made up of 2x4 Legos, measuring 24 pieces wide by 48 pieces long and an unspecified number of pieces tall. I have created a texture displaying this pattern using random colors: https://i ...

Tips for obtaining Array elements within a JSON object utilizing handlebars

var express = require('express'); var router = express.Router(); var data = { products :{ name:'Computers', price:'500' }, items:[ {name: 'Keyboard' , cost:'50'}, {name: 'M ...

Ways to use the v-if directive to render conditionally based on a property, even if the object may be undefined

Keep in mind that the parent variable is a firebase reference that has already been defined. Using it like this works fine: v-if="parent['key'] == foo" However, when trying to access a child element like this: v-if="parent['key'].ch ...

What are the steps to utilize webpack for refreshing the script tag in a jade file?

Can webpack be used to automatically update the script tag in a jade/pug template to point to the minified and cache-busted JS bundle? Most examples I've come across demonstrate how plugins can be used to convert the jade template to HTML, but I am us ...

Stop the page from scrolling when a modal is displayed in React

I've encountered an issue with my custom modal component where the background stops scrolling when the modal is open. Initially, I attempted to solve this by using the following code: componentDidMount() { document.body.style.overflow = 'hi ...

Issue with Lazy Loading in Angular 4 Universal

I recently created a new Angular CLI project to delve into server-side rendering with Angular Universal. Everything was set up and running smoothly, until I decided to implement lazy-loading for a module named 'lazy'. After implementing lazy-lo ...

How to implement an Angular component with a gauge using AngularJS Gauge JS

I have attempted to incorporate angular gauge js into my application using an angular component, but unfortunately it is not functioning as expected... The gauge chart can be found at Here is the code snippet I am working with: (function(app) { ...

Having trouble navigating the maze of Express routing

app.js file // home route app.get("/home", async(req, res)=>{ let allCards = await Card.find({}); let skills = await Skill.find({}); res.render("index", {allCards, skills}); }) // add new skill route app.get("/home/newskill", (req, res)=& ...

Retrieve the selected option from the dropdown menu in the specified form

What should I do if I have numerous products and want users to be able to add new dropdown boxes dynamically? When the submit button is clicked, only the value of "category[]" within the form should be retrieved. https://i.stack.imgur.com/v1fnd.png Below ...

What is the proper method for setting up handlers in functional React components?

My knowledge of JavaScript tells me that there are three different ways to define functions. Let's take a look at them: 1. Declaration function handleEvent(e) {} 2. Assignment var handleEvent = function(e) {} 3. Arrow var handleEvent = (e) => ...

Experience the exciting features of the Play! framework Morphia module for grouping data by date buckets

I've been working with the Play! framework morphia-mongodb module and I'm impressed with its built-in group aggregation functionalities. However, all the examples I've come across demonstrate grouping/aggregating by a fixed field, whereas my ...

Exploring Vue.js: Accessing nested elements

Trying to convert this to vuejs has presented some challenges. First off, How do I access nested sub elements? <div ref="tickerwrapper" class="tickerwrapper"> <ul ref="list" class='list'> <li re ...

Combining two classes into a single class using ‘this’ in JavaScript

I'm encountering an issue where I am unable to figure out how to extend from the third class. So, I really need guidance on how to call the A class with the parameter 'TYPE', extend it with C, and then be able to call getType() with class C. ...

An HTML attribute that evaluates to true if a condition is met

In my ng-repeat, each repeated item contains an input box where autofocus is set. This causes autofocus to be applied to all items, but in iOS, there's a bug that auto focuses on the last input box instead of the first one. To fix this issue, I need ...

Is there a simple method to transform a JavaScript object into pairs formatted as "name": "Fred", "value": "Blue"?

I'm trying to find a solution for converting an object into a specific string format using JavaScript. Here is the object in question: Object name: Fred lastname: Jones city: Los Angeles The desired output should look like this: // Do want this [ ...

Building a custom order creation system for Paypal shopping carts with personalized user information

I have a shopping cart where I store an array of my objects. Using a form and jQuery, I serialize all the custom user details. My goal is to integrate the user data gathered from the form (perhaps using payer or payer_info object) and also add my items to ...

What is the best way to conceal a bootstrap directive tooltip in Vue.js for mobile users?

Currently, I'm tackling a project with Vuejs and Laravel. There's a tooltip directive incorporated that relies on Bootstrap's functionality. Check it out below: window.Vue.directive("tooltip", function(el, binding) { // console ...

Ensure all checkboxes are selected in AngularJS

As a newcomer to AngularJS, I need some assistance with the following: https://i.sstatic.net/B0K2J.png When the user clicks on the "All" link, I want all 11 checkboxes to be checked. Conversely, when the user clicks on the "None" link, I want all 11 chec ...