The importance of scope in ng-style

I am attempting to dynamically change the font color in an input field based on the value entered into that field. However, I have encountered an issue with using ng-style as it is not recognizing the value of the scope and therefore the color does not update.

Here is the code snippet I utilized:

<input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="{'color': {{saldo}} < 0.00 ? 'red' : 'green') }">

This error message is what I am receiving:

Error: [$parse:syntax] Syntax Error: Token '<' not a primary expression at column 12 of the expression [{'color':  < 0.00 ? 'red' : 'green') }] starting at [< 0.00 ? 'red' : 'green') }].

So my question is, how can I successfully access and utilize the 'saldo' value within the ng-style? (placing {{saldo}} outside the ng-style displays the correct value)

Answer №1

Here is my suggestion:

<input disabled name="balance" type="text" class="form-control" 
       placeholder="0.00" ng-model="balance" 
       ng-style="balance < 0.00 ? {'color':'red'} : {'color':'green'}">

I hope you find this helpful.

Best regards

Answer №2

Using {{}} in ng-style is unnecessary.

Instead of:

<input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="{'color': {{saldo}} < 0.00 ? 'red' : 'green') }">

Use this approach:

<input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="{'color': saldo < 0.00 ? 'red' : 'green') }">

Answer №3

Here is a useful code snippet for you:

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

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.saldo = 1;
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="GreetingController">
<input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="saldo < 0.00 ? {color: 'red'} : {color: 'green'}">
  </div>
</div>

Answer №4

If you want to dynamically change the font color in an input field based on its value, here is a sample code snippet you can use:

<input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="{'color': saldo}">

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

How can I design a custom status update box inspired by the look and feel of Facebook

I have searched extensively online for a guide on creating a CSS box similar to Facebook's status update callout box. This box features a square shape with a small notch at the top. Could someone assist me in crafting a CSS design for this type of bo ...

How can JHipster components be effectively shared with other projects for optimal usage?

Exploring JHipster, the Spring Boot + AngularJS application generator based on Yeoman has been an interesting and fun experience. Setting up a vanilla webapp was relatively easy. Now, the goal is to separate entities, repositories, and services from the o ...

Harnessing the Power of Material UI to Revolutionize Amplify Theming

I am currently developing a React application with Material-UI and Amplify UI Components. I am looking to customize the Amplify theming. By following the guidance provided on Amplify UI Components Customization, I have been able to override CSS variables ...

How to Customize the Menu Style in Your WordPress Theme

As a newcomer to Wordpress theme development, I am struggling to properly style my navigation menu. Below is the raw HTML code I have: <!-- Navigation --> <nav class="navbar navbar-default navbar-custom navbar-fixed-top"> <div cl ...

Issues with implementing Bootstrap tabs in Vue application

Currently, I am in the process of developing an application using vue, vue-router, and bootstrap. My goal is to integrate tags in bootstrap as shown below: <ul class="nav nav-tabs" role="tablist"> <li class="nav-item"> <a class="nav-l ...

What makes this CSS causing render blocking?

I've been meticulously following the Google PageSpeed guidelines in order to optimize the performance of my website. Upon running an analysis, Google provides me with a score based on their set guidelines. There's one particular guideline that ...

The dropdown menu is obscured by the toolbar

When using ionic4/angular, I want a dropdown menu to display upon clicking on the ion-avatar. However, the dropdown menu is hiding in the toolbar. I have tried setting z-index but it did not work. Any suggestions would be appreciated. You can find the sta ...

How to display a video with full height and width using CSS or JavaScript

I am trying to incorporate an html5 video (using the video tag) with 100% width and 100% height to play in the background. I have seen an example using an image, but I want to achieve the same effect with a video. Here is the code I have so far: #video { ...

Scaling Flexbox divs based on images dimensions

Check out this cool example page When resizing the window in Firefox, Internet Explorer 10, and Opera, the images remain at their current sizes without any scaling. However, Chrome shrinks everything down proportionally as desired. It's unclear whet ...

What steps should I take to address these styling challenges?

Recently, I came across some code that uses the hspace attribute which is now considered outdated. In order to create space between elements in a widget, I am looking for a solution that involves adding 10px of space above and between the items using CSS. ...

What are the best methods for detecting an invalid URL?

Currently, I'm constructing a basic HTML website using Angular JS ui-router. To handle scenarios where users input invalid URLs like 'www.mysite/#/invalidUrl', I've set up a custom 404 page ('www.mysite/#/error/404') with the ...

Only referring to a CSS class within a particular element

I have defined the following CSS: .grade a{ color: white; } It is working, but maybe too well... Now, I have an HTML structure like this: <li class="grade"> <a><i class="fa fa-star fa-fw"></i></a> My Text < ...

Tips for customizing the background of form inputs in React-Bootstrap

Currently, I have a next.js project with react-bootstrap. I am attempting to change the default blueish gray background color (#e8f0fe or rgb(232, 240, 254)) that bootstrap uses for form inputs to white. To achieve this, I am customizing the bootstrap var ...

Leverage the power of JSON objects within your Angular.js application

Is it possible to request a JSON object, such as the one in employes.json file: {"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ]} Then, how can I util ...

Tips for relocating a CSS button

I have designed two buttons using css and I am looking to align them below the title "forecast customer activity". Due to extensive styling in css, the code might appear lengthy. Requesting assistance with a solution after reviewing the following code snip ...

Ways to create a self-contained video viewer

Is it possible to create a self-contained video player similar to jwplayer or the YouTube video player using just HTML, CSS, and JavaScript? I know that I can build a video player by utilizing the video tag along with some custom javascript and css, but ho ...

Varied elevations dependent on specific screen dimensions

There seems to be a minor issue with the height of the portfolio container divs at specific window widths. The problematic widths range from 1025 to 1041 and from 768 to 784. To visualize this, try resizing your browser window to these dimensions on the fo ...

Eliminating Unnecessary Stylesheets in Vite and Vue Website

When working on the Vite-Vue application, I noticed that the styles I implemented for both index.html and Vue components/views are showing up as crossed out in the browser. Additionally, many of the styles I added to the components/views are also being cro ...

Inquiry about CSS Media Queries

I am facing an issue with CSS media queries... For my HTML page, I have separate files for desktop and iPad styles - desktop.css and ipad.css. In desktop.css, I have used @import url("ipad.css"); and added a @media not only screen and (device-width:768px) ...

Prevent the display of HTML tags in ASP.NET

Looking for a way to prevent a specific HTML tag from being printed in my ASP.NET project view file, whether through a printer or to a PDF file. Are there any HTML or CSS attributes that can help achieve this? How should they be configured? I have alread ...