Use ng-class in a P tag to assess a variety of expressions

Is there a way to apply ng-class to automatically evaluate negative values within a < p > tag?

<p <strong>LW$:</strong> {{d.lw_metric}} <strong>LW:</strong> {{d.lw_metric_percentage}} <strong>L4W:</strong> {{d.l4w_metric}}</p>

I currently have the following CSS:

.positive{ color: green}
.negative{ color: red}

However, I am unsure how to instruct Angular to evaluate all the values inside the tag without specifying each one individually like this:

ng-class = "{'positive':data.merchMetrics.LW_CHG_LY >=0,'negative':data.merchMetrics.LW_CHG_LY <0}"

There are five expressions within this tag, so I believe there might be a more efficient approach.

Answer №1

It is possible to transfer that logic over to the controller for better organization.

ng-class="evaluateClass(data.merchMetrics)"

Controller

$scope.evaluateClass = function(merchMetrics){
    var returnClass = "";
    //The following loop will go through each property of the object
    //and determine which class needs to be applied
    for (var key in merchMetrics) {
       if (merchMetrics.hasOwnProperty(key)) {
          //Additional logic can be added here
          //so that the string will have positive and negative classes once
          if(merchMetrics[key] > 0)
             returnClass = returnClass + "positive "
          else
             returnClass = returnClass + "negative "
       }
    }
    //The class can be returned from here
    return returnClass;
};

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

Loading Ajax content on a webpage

Recently, I've been impressed with the layout of Google Play Store on a web browser. I am eager to replicate that same smooth browsing experience on my own website. Specifically, I want to create a feature where selecting a category or item doesn&ap ...

Can you provide instructions on creating a marker like this in an unordered list?

Is there a way to create a bold line like this within a bulleted list easily? https://i.sstatic.net/pw8Zt.png I've tried using the border property, but it's not showing up correctly. Any quick and simple solutions for achieving this effect? Th ...

Guide on using javascript to alter a json structure

Recently, I discovered a method to manipulate a JSON file using JavaScript. Although I don't have any formal training in JavaScript, I understand that it is primarily utilized on web browsers. Can someone guide me on how to execute this script? I cons ...

The element's height appears to be fluctuating unexpectedly when I attempt to adjust it using percentage values within a narrow range

I'm utilizing React and Bootstrap in this project. Here's an overview of my code: I have an element with height set to 0, in rem. My goal is to make the height of this element increase as I scroll down the page, creating the illusion that it is ...

Can a child element be positioned above its parent's y-scrollbar using CSS?

I have created a selection box using <ul> and <li> elements. Some of the list items are wider than the parent box, so I used an :after pseudo-element to overlay them with full content when hovered over. Everything was working perfectly, until ...

broadcast updated $rootScope

How do I update the $rootScope.$broadcast with a new value? Let's take a look at this code snippet: var test = "fisk"; if(angular.element(this).hasClass('monster')) { var monster_info = angular.element(this).find("img").attr("title"); ...

Access inner element with AngularJS directive

I have the following directive: (function() { 'use strict'; angular .module('myApp') .directive('inner', inner); function inner () { return { restrict: 'A', ...

What steps are involved in creating a circular shape on canvas?

I am trying to create a circular shape in the canvas using the code below. The code involves moving an object with keyboard keys, and I need help making the canvas shape into a circle without affecting the functionality of the code. I attempted to modify t ...

Resolving duplicated controller code in Angular using UI.Router

During the process of updating my app to utilize UI.Router's state.resolve in order to streamline some controller logic, I encountered a common issue: How can one prevent code duplication that occurs when fetching dependencies in resolved assets and u ...

Can TypeScript be used to dynamically render elements with props?

After extensive research on SO and the wider web, I'm struggling to find a solution. I have devised two components, Link and Button. In short, these act as wrappers for <a> and <button> elements with additional features like chevrons on t ...

Positioning of bottom navigation bars

Is there a way to ensure that the bottom navigation bar remains fixed at the bottom of the screen, regardless of the device's screen size? position: absolute; bottom: 0; width: 100%; While this solution works, it has been observed that when th ...

What steps can I take to maintain the width while implementing jQuery's slideUp function?

I am facing a scenario where: http://www.jsfiddle.net/kPBbb/ After adding content to .content, the .wrapper shrinks itself, possibly due to the fact that the .content is set to display: none. I want the .wrapper to maintain its original size even after t ...

What is the best way to activate an @keyframe animation based on the scrolling action?

I am currently working on an @keyframe animation that rotates a circle along its x-axis, starting from 0 degrees to 90 degrees and then back to 0 degrees. My objective is to synchronize the rotation of the circle with the user's scrolling movement on ...

Issue with handling click events on action column in Datatable using jQuery

Utilizing jquery datatable with an action column containing "edit" and "delete" links. The table populates correctly, but encountering an issue when trying to open a bootstrap modal form upon clicking the edit button within the table. However, the modal do ...

Infinite scroll causing Firebase ".length" function malfunction

My NextJs website is encountering errors related to Firebase infinite scroll. The issue seems to be with the .length property being undefined for some unknown reason. I am struggling to debug the code and make it work properly in Next.js. Any help would be ...

Return to the beginning using Jquery Mobile

Currently, I am working on developing a mobile web app using jQuery Mobile. I am now looking to implement a back-to-top action. Typically, this can be done with the following code snippet: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

What's the best way to ensure my website's footer spans the entire width of the webpage?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Elena Brown | Graphic Designer</title> <link rel="stylesheet" href="CSS/normalize.css"> <link href=' ...

I keep receiving multiple header errors from ExpressJS even though I am positive that I am only sending a single header

Can someone please help with the issue I'm facing in the code below: router.put("/:_id", async (req: Request, res: Response) => { try { // Create the updated artist variable const artist: IArtist = req.body; const updatedArt ...

Equal gutters are present between CSS floats

After conducting various experiments, I have devised a method to incorporate fixed and equal gutters between floats. My approach involves using positive and negative margins as well as multiple wrappers, making the solution somewhat cumbersome. However, I ...

Angular 6: Harnessing the Power of Subject

In my angular applications, I have been utilizing the Subject feature from the rxjs library to create an event emitter. However, upon migrating to Angular 6, I encountered the issue that this module is no longer available. Cannot find module 'rxjs/Su ...