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

JavaScript function unable to execute form action properly

I have a link RESET YEAR which triggers a servlet to check if the current year is equal to the present year. If they are not equal, then the function resetyear() is supposed to be called. The issue I am facing is that the function is not working as expecte ...

What is the best way to apply a CSS class to a div element without affecting its child elements using JavaScript?

Currently, I am using JavaScript to add and remove a CSS class through a click event. Within my div structure, I have multiple similar divs. While I can successfully add and remove the class from my main div, I am facing an issue where the class is also ap ...

Bootstrap 3's Clear:Left Media Query Feature

Bootstrap is being used, and there is a div with col-xs-12, col-sm-6, col-md-4 classes. Task : To add a clear on every 1 item for col-xs-12, add a clear on every 2 items for col-sm-6, and clear on every 3rd item for col-md-4. The current code only s ...

Refresh a row in real-time by utilizing a modal with JavaScript or jQuery

Is there a way to dynamically edit and update a previously submitted row (category name) in a table? I am able to edit a row by clicking on an edit button and displaying a modal with the current value. However, I am facing a challenge when trying to submit ...

Ways to verify whether a vue instance is empty within a .vue file by utilizing the v-if directive

I am facing an issue with a for-loop in Vue that iterates through a media object using v-for to check if it contains any images. Everything is working correctly, but I want to display a div below the loop saying "There is no media" when the object is empty ...

Filtering a list with Vue.js using an array of checkboxes

Currently, I am exploring ways to filter a v-for list using a checkbox model array with multiple checkboxes selected. Most examples I have come across only demonstrate filtering one checkbox at a time. In the demo here, you can see checkboxes 1-3 and a lis ...

Tips for simultaneously updating a value in multiple collections on firestore?

Currently, I am in the process of developing a forum application using Vue, which is essentially a replica of this platform. In this app, users can post questions, receive answers to those questions, and leave comments on those answers. Each question, answ ...

Performance issues arising from the slow loading of multiple mat tables within a single component

Within a single component, I have 3 mat tables and I am loading their data on page load. Even with fewer than 500 records for all 3 tables, it takes 8-10 seconds to load the data. <div id="div1" [hidden]="!( div1===1 ...

Is there a way for me to update the button text and class to make it toggle-like

Is there a way to switch the button text and class when toggling? Currently, the default settings show a blue button with "Show" text, but upon click it should change to "Hide" with a white button background. <button class="btn exam-int-btn">Show< ...

Tips for designing a HTML form using classes

I need help styling this form using CSS because I have similar areas like a search submit button that require different classes. Any assistance would be greatly appreciated. <perch:form id="contact" method="post" app="perch_forms"> <perch:cont ...

Dynamic headers with $save in AngularJS $resource

I have been attempting to include dynamic headers in a $resource as shown below: angular.module('app') .factory('API', function ($resource, API_URL) { return { event: function(userId){ return $resource(API_URL + '/event/ ...

What is the best way to achieve a precision of 6 decimal places in JavaScript when working with decimals?

While working on coding to round numbers to six decimal places after performing some arithmetic operations, I encountered a problem. I was iterating through the elements of an array and conducting calculations based on the array contents. To achieve roundi ...

Having trouble exporting data from Excel using react-data-export

I've been attempting to create an excel export feature, but for some unknown reason, it's not functioning properly. I'm utilizing react-data-export and followed a small test example from this GitHub link, yet the export is not working as exp ...

Navigate through the contents of a table featuring intricate colspan and rowspan elements, while keeping both headers and left columns in

I am facing a challenge with a dynamically generated table that has complex structure including colspans and rowspans. My goal is to fix the headers and, if possible, even lock the first few rows on the left side while allowing the content of the table to ...

Is it just me, or does `position: fixed;` not actually fix the element to the top?

I'm experiencing an issue where the element isn't fixed to the top of the page, despite using the position: fixed; property. I've tried several solutions including: Setting a z-index of 9999 Wrapping all elements in a single container (.fi ...

Error: authentication failed during npm installation due to an incorrect URL

After executing npm install @types/js-cookie@^2.2.0, an error occurred: npm install @types/js-cookie@^2.2.0 npm ERR! code E401 npm ERR! Unable to authenticate, need: Basic realm="https://pkgsprodsu3weu.app.pkgs.visualstudio.com/" npm ERR! A com ...

Utilizing the "::first-letter" pseudo element selector in conjunction with the MuiButton class in Material UI - What is the process?

Is it possible to change the text case of Button text inside Material UI Button to sentence case with CSS? https://mui.com/components/buttons/ The text inside the button - ADD FRIEND should be transformed to sentence case as Add friend. I attempted to o ...

How can I divide AngularJS ng-grid into two separate columns?

I am trying to extend the ng-grid to display data from one column into two columns: $scope.gridOptions = { data: 'myData', columnDefs: [{ field: "name", width: 120}, { field: "age", width: 120 }] }; When ...

Discover captivating visuals that effortlessly occupy the entire breadth, while gracefully encompassing a mere quarter of the total vertical space on the

It's become quite challenging for me to locate images that can occupy the entire width of a page while maintaining just 25% of the page height. Whenever I make adjustments to the image's CSS, it ends up appearing distorted because its width is di ...

I'm searching for a universal guidebook on creating web page layouts

After 5 years of creating webpages, I have realized that my sites tend to have a nostalgic 1995 internet vibe. Despite being a C# programmer with knowledge in HTML, JavaScript, and CSS, my design skills could use some improvement. Is there a quick referenc ...