Achieving both positive and negative styling in AngularJS with ng-class: A guide

I'm currently working on an application that requires indicating red for negative values and blue for positive values in a calculation.

<td class="amount debit">
    <input type="text" class="form-control" ng-model="vm.model.form.amount_debit">
</td>
<td class="amount credit">
    <input type="text" class="form-control" ng-model="vm.model.form.amount_credit">
</td>
<td class="amount balance" ng-class="{
    negative : (book.total_balance + +vm.model.form.amount_debit - +vm.model.form.amount_credit) < 0.00, 
    positive : (book.total_balance + vm.model.form.amount_debit - +vm.model.form.amount_credit) >= 0.00
}">
    @{{vm.model.book.total_balance + +vm.model.form.amount_debit - +vm.model.form.amount_credit| currency:""}}
</td>

The amount in the balance field is calculated whenever the user inputs a value into either input field. However, there seems to be an issue with how the output is displayed:

When I enter a value in the amount debit field, it displays as a positive value. But when I enter a value in the amount credit field, it shows me a negative value.

This discrepancy does not accurately represent the intended output I am looking for.

For example, if the balance is $1,000 and the amount debit is $300,

it correctly shows a total of $1,300 in positive.

However, if the balance is still $1,000 but the amount credit is $300,

the total incorrectly shows as $700 in the negative. It should have remained positive.

Could there be an error in my logic somewhere? Where can I make adjustments to fix this issue? Thank you.

Answer №1

For illustrative purposes, I have put together a sample here http://jsfiddle.net/ADukg/12791/

<table>

<td class="amount balance">
    <input type="number" class="form-control" placeholder="balance" ng-model="total_balance" />
</td>
<td class="amount debit">
    <input type="number" class="form-control" placeholder="debit"  ng-model="amount_debit" />
</td>
<td class="amount credit">
    <input type="number" class="form-control" placeholder="credit"  ng-model="amount_credit">
</td>
<td class="amount balance" ng-class="{
    negative : (total_balance - amount_debit + amount_credit) < 0.00, 
    positive : (total_balance - amount_debit + amount_credit) >= 0.00
}">
    @{{total_balance - amount_debit + amount_credit | currency:""}}
</td>

</table>

To avoid the hassle of converting strings to floating point numbers, I opted for using `input type="number`.

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

What is the best way to assign a unique color to each circle?

Struggling to assign random colors to each circle in my canvas. Currently, they all have the same color that changes upon refresh. I'm aiming for unique colors on each circle but my attempts have been unsuccessful so far. Check out my code below: v ...

What is the reason for node-sass being rebuilt after upgrading from npm6 to npm7?

My application utilizes sass opposed to node-sass. Within my package-lock.json file, there is no mention of node-sass. I have been successfully using npm 6 for years without any issues. However, upon attempting to run npm install with npm 7, I encounter ...

Guide on retrieving POST data in sveltekit

Hello, I'm new to sveltekit and I am trying to fetch post data in sveltekit using a POST request. Currently, I am utilizing axios to send the post data. const request = await axios .post("/api/user", { username, email, ...

Utilize the Math.log() function within an HTML document

I am attempting to incorporate a Math operation in an HTML page, but unfortunately it is not functioning as expected. Here is the code snippet that I have experimented with. Is there any solution to make this code operational? HTML code <div> < ...

In Typescript, try/catch blocks do not capture return values

I am currently working on a function that performs database operations, with the implementation contained within a try/catch block. Here is an example: async function update({id, ...changes}): Promise<IUserResult> { try { //insert code here retu ...

The initial execution of the getDocs function may encounter some difficulties

Whenever a user connects from localhost:3000/ (which automatically redirects to /profile), I try to fetch all documents from Firebase. However, it does not work on the first attempt. Strangely, upon refreshing the page, it successfully retrieves the docume ...

Enhancing React Native experience by dynamically editing array elements

This code block defines the state of my program, including an array: this.state = { arr: [{ arrid: 0, arrEmail: '', arrName: '', arrPhone: '' }], id: 0, email: "", isChecked: true, name: "", phon ...

Guide to decoding JSONP data sent from a remote server

Working on retrieving data using JSONP. After observing the returned data in Firebug, I can confirm that it is being returned correctly. However, I am facing a challenge in figuring out how to parse it. Is the data returning as a nested array? The callback ...

Would it be possible to use a script to convert the y-axis values of the chart into Arabic numerals?

Currently, I am working on creating line and pie charts, but I need the Y-axis to display Arabic language. $('button').on('click', function(e) { $('#pie-chart3').empty(); var type = $(this).d ...

Assess html code for Strings that include <% %> tags along with embedded scripts

Having a small issue with my code where I am receiving an HTML response from a web service as a java String. I need to display this String as HTML on my webpage, but the problem is that there are some script tags in the form of <% ... %> which are sh ...

The international telephone input library's "grunt img" command is malfunctioning

I am currently utilizing intl-tel-input to develop a control for displaying mobile numbers. Since the flags are quite small, I am in need of enlarging them. A reference for this can be found here: https://codepen.io/jackocnr/full/ONXWgQ To achieve this, ...

Creating endless scroll feature in Vuetify's Autocomplete component - A comprehensive guide

Having trouble with my Vuetify Autocomplete component and REST API backend. The '/vendors' method requires parameters like limit, page, and name to return JSON with id and name. I managed to implement lazy loading on user input, but now I want i ...

Update the HTML weather icon with data from JSON

I have a collection of weather icons stored on my computer. How can I customize the default weather icons with my own set of icons? In the JSON file, there is a variable like this: "icon":"partlycloudy" <html> <head> <script src="http://c ...

An error has been caught in the Angular JS application while using the highcharts-ng module: [$injector:modulerr]

I'm encountering an issue while trying to integrate Highcharts into my Angular/Node/Express application. I am utilizing the highcharts-ng directive available at https://github.com/pablojim/highcharts-ng Every time I run my app, I consistently receive ...

The JSON.stringify method is failing to correctly calculate the length of the JSON array

Hey there, I'm trying to implement an Excel upload feature that saves data into a MongoDB database. I've managed to convert the Excel cell values into JSON format and parse it successfully. However, I'm facing a challenge in determining the ...

Tips for creating a secure authentication system in an AngularJS application!

As a novice in the world of angularjs... After going through the documentation and completing a tutorial, I decided to experiment on my own which has helped me grasp things better. Now, I'm looking into creating a secure authentication system. The ...

Having trouble getting the node serialport module to function properly when using the child process fork()

My current project involves implementing an asynchronous read in USB using fork() from the child_process module in electron. Essentially, upon clicking a div (id="read"), a message is sent from the renderer process to the main process. The main process the ...

Having trouble getting Node.js to run Express.js and React.js simultaneously

My tech stack consists of reactjs for the frontend and expressjs for the backend API. I experimented with the following setup: { "name": "carweb", "version": "0.1.0", "private": true, "dependencies": { // list of dependencies }, "scripts ...

Troubleshooting the NullInjectorError in Angular - Service Provider Missing?

I'm facing an issue in my code where I have buttons that should trigger pop-ups displaying details as a list when clicked. However, every time I click the buttons, I encounter the error mentioned below. It seems like I am unable to access the desired ...

Is there a way to store a file in a server directory using the <a href='mypdf.pdf'> tag?

I find myself in a bit of a bind. I have a document that needs to be saved in my server directory, which is attached in an <a href='mypdf.pdf'>My pdf</a> tag. The issue is that the filename, mypdf.pdf, is dynamically changing via Jav ...