Customizing background styles in real-time in a Meteor-Angular application

I'm currently working on a unique AngularJS Ionic Meteor application, and I am in search of a method to dynamically change the background color of the bottom box within an ionic card based on specific float values. The criteria for color changes are as follows:

data<=80
81 < data <= 160
161 < data <= 233
234 < data<= 317
318 < data <= 400.

Are there any CSS or AngularJS techniques available to achieve this functionality?

Answer №1

To apply conditional styling in AngularJS, you can utilize the ngClass directive. Define your CSS classes with desired background-color properties and use them based on certain conditions set in your controller. Here's an example:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
  $scope.submit = function() {
    if ($scope.data <= 80) $scope.rangeColor = "red";
    // Add more conditional statements here
    else $scope.rangeColor = "blue";
  }
}
.card {
  border-style: solid;
}
.red {
  background-color: red;
}
.blue {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <h4>Data:</h4>
    <form ng-submit="submit()">
      <input type="text" name="data" ng-model="data" required>
      <input type="submit" id="submit" value="Submit" />
    </form>
    <br />
    <div ng-class="rangeColor" class="card">
      <div class="item item-text-wrap">
        This is a basic Card which contains an item that has wrapping text.
      </div>
    </div>
  </div>
</body>

Alternatively, you can directly implement the conditional statements in your HTML elements like so:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {

}
.card {
  border-style: solid;
}
.red {
  background-color: red;
}
.blue {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <h4>Data:</h4>
    <input type="text" name="data" ng-model="data" required>
    <br />
    <br />
    <div ng-class="{'red': data <= 80, 'blue': data > 80}" class="card">
      <div class="item item-text-wrap">
        This is a basic Card which contains an item that has wrapping text.
      </div>
    </div>
  </div>
</body>

Answer №2

If you're looking to style elements dynamically in AngularJS, you have a couple of options:

 ng-style

For more information on how to use ng-style, check out the official documentation here.

Alternatively, you can also use

ng-class

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

Display a single div and conceal the rest upon clicking a link

Trying to find a way to hide all other divs when clicking on a navbar link to toggle them. Currently, the divs stay active when a new link is clicked. The goal is for them to slide up and the clicked one to slide down. <script type="text/javascript> ...

SwiperJS continuously applies additional right margin to every slide

My Swiper carousel seems to be adding an unnecessary 5px margin-right to each slide, resulting in the cards not fitting into one frame. https://i.sstatic.net/fwn5G.png I attempted to fix this by setting the margin-right to 0px for the .swiper-slide class ...

What would be the counterpart of setLocale in Yup for the zod library?

How can I set default custom error messages for Zod validation? If I want to use i18n for error messages in Yup, I would do the following: import { t } from "i18next"; import * as yup from "yup"; import "./i18next"; yup.setL ...

What is preventing the use of data post submission in React JS?

After posting data to the navigation, I retrieve it and then use the following code: .then(data => console.log(data.PathPDF)) .then(data => window.open(data.PathPDF, '_blank', 'noopener,noreferrer')); While I can successfully ret ...

Passing a parameter from a redirect function to an onClick in React using TypeScript

I am facing a challenge in passing a parameter to my redirectSingleLocker function. This function is intended to take me to the detailed page of a specific locker, identified by a guid. The lockerData.map method is used to display all the JSON data in a ta ...

I'm seeing a deluge of errors in the console every time I attempt to utilize ng-change. What could be causing

These errors appear to be related to the regularInterceptedExpression. The goal is to enable users to paste properly formatted JSON into the left column text area, have it automatically parsed, manipulated, and displayed in the right column. Currently, t ...

Error in Javascript when attempting to embed Redbubble

Hello, I am currently working on embedding the redbubble code into a Joomla site for a client who has a Redbubble store. However, I seem to be encountering some issues with the script. <script type="text/javascript" src="http://www.redbubble.com/asse ...

Assigning a new classification to the primary object in the evolving array of objects

I'm working with an element that loops through all the objects using v-for and has a CSS class named top-class{}... I need to dynamically add the top-class to the first object (object[0]) and update it based on changes, removing the old top-class in t ...

Translating a few lines of JavaScript into C#

I'm looking to convert some code from JavaScript to C#, but I'm having trouble grasping a certain section... function getHisto(pixels) { var histosize = 1 << (3 * sigbits), histo = new Array(histosize), inde ...

Decode my location and input the address before validating it

While I have come across numerous plugins that enable geolocation and display it on a map, I am searching for something unique. I am interested in implementing geocoding when a user visits the page with an option to "allow geolocation." If the user agrees ...

Verify the text file for any data, and if it contains any, display it on the web browser using JavaScript

I have a program in C that works with a temperature sensor. It creates a file to store the temperature and indicates whether it falls within specific values. I want to display this data on a web browser and update it every 5 minutes. I'm looking for ...

Creating a pie chart with a legend in the realm of the dojo

Apologies for any language errors. I am looking to develop a web application where users can fill out a form and submit it to the server. The server will then respond with the requested data in JSON format. Using this data, I want to create a diagram and ...

Combining HTML/CSS to overlay text on an image - EMAIL

Why does Google keep stripping my <style> tag when I try to overlay text on an image? I want the image to be on the left with two text blocks positioned over it on the right (one higher and one lower). It looks fine visually, but when I send it to m ...

What could be the reason for the malfunctioning of my React Native vector icons?

My react native vector icons are not working despite following all the steps mentioned in the documentation. I am unable to use a camera icon in my app. I tried importing { Entypo } from 'react-native-vector-icons'; and then using \<Entyp ...

Seeking a window-adjustable table with stationary headers

I have been searching high and low for a solution to my issue but have come up empty-handed. My goal is to create a table with fixed headers that remain visible while scrolling through the rest of the table. The catch is, I also need it to align properly w ...

Changing environment variables for jasmine tests

My Angular service code snippet includes importing the environment like this: import {environment} from '../environment' .... public something() { if(environment.production) { // do stuf } else { // do something else } } I am now l ...

The presence of certain characters is leading to problems in the console

Similar Question: Escaping a String for JavaScript Usage in PHP? The characters in my text are causing errors. When I input special characters such as: !\"$%^&()-=\'.,:;/?#~/\\>< An error saying "Syntax error ...

To center a div vertically within a Bootstrap 4.1 container-fluid > row > col, you can use the following method

I'm currently working on incorporating Bootstrap 4.1's align-middle into a col nested within a row. Despite my efforts, the content within is not aligning to the middle as expected. Take a look at my code below: <body class="d-flex"> < ...

The failover process for PayPal involves seamless transitions to backup systems in the

I am currently assisting a client with a unique architecture setup: Back End Running on Java Server Front End Powered by Proxy (Node JS + Express application) For security reasons, all requests made to the server must pass through the Proxy. We ar ...

Issue with hashtag in regular expressions

Looking for a way to identify and link hashtag words with an anchor tag? Here's a snippet showcasing the concept: <p class="display"></p> var content = "I enjoy #blueSky. My favorite color is #green. #sky is amazing"; ...