What is the best way to dynamically update my CSS based on the value of JSON data?

If the value of one of my keys is Green/Red, I would like to display a bootstrap button with the class btn-success when its value is green and btn-danger when it's red.

Answer №1

Your ng-class attribute needs to be updated:

<div ng:controller="CartForm">
  <ul>
    <li ng-repeat='country in countries'><a class="btn" ng-class="{'btn-primary': country.color == 'green', 'btn-danger': country.color == 'red'}" href="">{{country.name}} - {{country.population}}</a></li>
  </ul>
</div>

In addition, the appliedClass function should be deleted.

View a working example on jsFiddle

Answer №2

Have you considered using ng-class? You can implement it like this:

<p ng-class="condition ? 'classIfTrue' : 'classIfFalse'">Foo</p>

In your scenario, it could look something like this:

<p ng-class="myVariable ? 'btn-success' : 'btn-danger'">Foo</p>

If you need a code example to understand how it functions, take a look at this one:

angular.module('app', []).controller('MyCtrl', function() {
    this.valueSuccess = false;
    this.valueDanger = true;
});
.danger { background-color: red; }
.success { background-color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<section ng-app="app">
  <div ng-controller="MyCtrl as vm">
    <p ng-class="vm.valueSuccess ? 'danger' : 'success'"> {{vm.valueSuccess}}</p>
    <p ng-class="vm.valueDanger ? 'danger' : 'success'">{{vm.valueDanger}}</p>
  </div>
</section>

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

"The error message appeared stating that the AngularJS Protractor element cannot be clicked at its current position due to another element

Hey there, I've been working on a simple AngularJS application and running into some trouble with Protractor in Chrome. Here's a snippet of my code: HTML: <div> <div> <ul...... <li.........../li> <li............./li> ...

Protractor synchronization with the page exceeded the time limit of 50001ms and timed out

I am currently testing an AngularJS application and I am relatively new to using protractor. Whenever I open the browser, it opens successfully but then waits for a timeout before displaying the following error in the command prompt. Protractor synchron ...

Creating a return type for JSON observables in Angular 2 by defining it as an interface or

I want to ensure that my JSON API response is easily organized into a class or interface so that I can always identify the attributes present. The JSON data I am working with is as follows: { "users": [ { "id": "bd3d70fd-03f7-4f5e-9ac1-4cb7221 ...

The imminent arrival of Postgresql JSONB has me wondering: Which data structure should be employed in the meantime - Hstore, JSON,

As I navigate through the research debate between relational databases and NoSQL, I have decided to move forward with PG as my data store. The introduction of JSONB in version 9.4 played a significant role in this decision. Now that I want to implement jso ...

My JSON file is constantly being overwritten by an AJAX request

I am encountering an issue where only the last JSON request is being saved to a file, which I suspect is causing my general.json file to be overwritten each time. My goal is to append all requests to general.json. Can someone please help identify the probl ...

Unable to successfully submit a form using PHP

I am currently working on a PHP page that retrieves data from MySQL. The goal is to fetch a list of objects from the database, display each object in a separate form for editing, and then save only the edited values. However, I am encountering difficulties ...

Using JSON.NET in C# to decode Twitter JSON and retrieve hashtags

Perhaps I am posing this question again, but this is my first attempt and I haven't come across anything quite like my situation.. As I code in C# and utilize JSON.NET to deserialize a JSON, the JSON file is retrieved from Twitter as a response to ...

What is the best way to center an element that completely fills the window?

Is there a way to ensure that the canvas in this demonstration remains centered at all times, even when scrollbars appear and the window is resized? The goal is for the canvas element to always fill the window. Despite my attempts to adjust settings like ...

Looking for assistance with a straightforward CSS opacity fix for Internet Explorer

Take a look at this page comparison between chrome and IE. On the right side, you'll notice drop down fields. While Chrome displays them correctly, IE shows rectangular boxes instead. To fix this issue, I added opacity to the select fields with the ...

When you try to create a popover, a cautionary message pops up indicating that $tooltip is no longer supported. It is

I need help with creating a popover that appears when hovering over an anchor tag. Here is the code I am using: angular: 1.4.2 ui-bootstrap :0.14.2 <div class="row" ng-repeat="endorsement in endorsements| filter: {category:categorySelected}"> &l ...

Facing issues with the functionality of a personalized AngularJS directive

I have created a custom directive using AngularJS 1.4.3. Below is the code for my directive: 'use strict'; angular.module('psFramework').directive('psFramework', function () { return { transclude: true, s ...

Utilizing Python to Retrieve JSON Information with Django

Whenever I try to extract data from JSON, I always encounter the error TypeError: unhashable type: 'dict' Here is my JSON information: u 'paging': { u 'cursors': { u 'after': u 'MTQyNzMzMjE3MDgxNzU ...

Is there a way to modify the background shade of an HTML meter element?

Can the background color of an HTML meter be customized? I noticed that by default, it has a green background. Is there a way to change this green background to a different color? I attempted using the style attribute with a red background color, but it ...

Toggle the visibility of three different div elements using JavaScript on mouseover

Trying to create three image links that display different divs when onMouseOver. <script type="text/javascript> function toggleVisibility(divid) { if (divid==="1"){ document.getElementById("1b").style.visibility = "visible"; document.getElem ...

Is there a way to align text in the middle while having different icons on each side?

Is there a way to center align the text in this image with minimal use of CSS/HTML? The icons on the left are causing it to be off center: https://i.sstatic.net/TKOYG.png Below is the relevant HTML and CSS for this top section: <div class="navbarhead ...

Color-coding HTML table cells depending on the SQL flag value

After successfully constructing a SQL query that incorporates three conditions from my database table and sets a flag based on them, I have made progress in my project. The query looks like this: UPDATE staging SET `miuFlag` =1 WHERE `lowSideMIUNumAr ...

CSS: The sub-class functionality is not functioning as intended

Having trouble with this HTML element : <span class="item-menu-notif non-lue" onclick="dosomething(2150)"> TEXT </span> These are the CSS classes : .item-menu-notif{ display: block; cursor: pointer; padding: 0 0.4em 0 0.4em; } s ...

Error: JSONResponse is missing a semicolon before the statement in the syntax

I encountered the following error: SyntaxError: missing ; before statement Although I am unsure of what is causing this error, here is the code snippet that I am currently working with: (function pollschedule(){ $.ajax({type: "GET", dataType: "j ...

Navigating through JSON using a variable in Postman loop

Within my code, I have stored a JSON response in a variable named savedData = {"ID":"{{ID}}","command":"ok"}. Additionally, I have created an array variable called ID = [1,2,3]. My goal is to iterate through the JSON based on the array variable and utilize ...

The issue with the <Button> component not properly linking in next.js

Having trouble with a button wrapped inside a custom Navbar component. The code snippet in question is: <NavBtn> <Link href="/contact" passHref> <Button> Contact Me </Button> </Link> & ...