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

New approach: AngularJS - Using nested ng-click events

Looking for a solution to a problem with my html code: <div class="outerdiv" data-ng-click="resetText()"> <div class="innerdiv" data-ng-click="showText()"> {{ text }} </div> </div> The outer div has an ng-click fun ...

The primary origin of TypeScript is derived from the compiled JavaScript and its corresponding source map

Being new to sourcemaps and typescript, I am faced with a project that has been compiled into a single javascript file from multiple typescript files. The files available to me are: lib.js (the compiled js code of the project) lib.js.map (the source map ...

Attempting to create a school project involving pizza, but encountering some technical difficulties

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>pizza ...

The ng-click function is failing to work on dynamically inserted HTML retrieved from an http.get request in AngularJS

Greetings, I am currently utilizing AngularJS for a PHP project. One of the features I have implemented is using ngclick for an anchor link, which looks like this: <a href="http://localhost/mediaads/signup" ng-click="showregister($event)">Don't ...

Discover the magic of observing prop changes in Vue Composition API / Vue 3!

Exploring the Vue Composition API RFC Reference site, it's easy to find various uses of the watch module, but there is a lack of examples on how to watch component props. This crucial information is not highlighted on the main page of Vue Composition ...

When attempting to call an XML node with JavaScript in an HTML file within Dreamweaver, the functionality works as expected. However, upon testing the same code on a server

As a beginner in Javascript, I am attempting to use document.write to display some XML data in an HTML format. Below is the code that I have been working on: <script> function loadXMLDoc() { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest( ...

``Protect your PDF Document by Embedding it with the Option to Disable Printing, Saving, and

Currently, I am facing a challenge to enable users to view a PDF on a webpage without allowing them to download or print the document. Despite attempting different solutions like Iframe, embed, PDF security settings, and Adobe JavaScript, I have not been s ...

Resolver for nested TypeORM Apollo queries

I've set up a schema that includes database tables and entity classes as shown below: type User { id: Int! phoneNumber: String! } type Event { id: Int! host: User } Now, I'm attempting to create a query using Apollo like this ...

The error message "ECONNRESET" occurred while attempting to send a post request using Axios to

Attempting to send a post request to my webserver using axios, I have a client that collects user input to populate an array of strings. This data is then sent via a post request using axios for processing by the server: if (parsedInput > 0 &&am ...

How can we create karma unit tests specifically tailored for Angular applications?

In the past, I utilized Jasmine and Karma to conduct unit tests on my AngularJS application. I am curious to find out if there is a method for dynamically generating Karma unit test files. Can someone advise whether it is beneficial to auto-generate unit ...

Transforming an ordinary JavaScript object into a class instance

As I was delving into Angular's documentation on "Interacting with backend services using HTTP", I came across the following statement in the "Requesting a typed response" section: ...because the response is a plain object that cannot be automatical ...

The nodejs events function is being triggered repeatedly

I have been developing a CMS on nodejs which can be found at this link. I have incorporated some event hooks, such as: mpObj.emit('MP:FOOTER', '<center>MPTEST Plugin loaded successfully.</center>'); Whenever I handle this ...

When attempting to access the Object data, it is returning as undefined, yet the keys are being

I have an Object with values in the following format: [ { NameSpace: 'furuuqu', LocalName: 'uuurur', ExtensionValues: 0, FreeText: 'OEN', '$$hashKey': 'object:291' }, { Nam ...

Running Controllers from Other Controllers in AngularJS: A Guide

Switching from a jquery mindset to Angular can be challenging for beginners. After reading various tutorials and guides, I am attempting to enhance my skills by developing a customizable dashboard application. Here is the HTML I have so far: <div ...

Create animated changes in height for a mdDialog as elements are shown or hidden

Utilizing Angular Material, I have implemented tabs within an md-dialog. The dialog smoothly adjusts its height based on the content of the active tab during navigation. However, when utilizing an ng-if directive to toggle visibility of content, there is n ...

exploring the ins and outs of creating computed properties in TypeScript

How can I store an object with a dynamically assigned property name in an array, but unsure of how to define the array properly? class Driver { public id: string; public name: string; constructor(id , name) { this.id = id; th ...

What is the process of transforming an object type into a two-dimensional array using lodash?

In order to properly display multiple tables in my Angular project, I am looking to convert an object type into an array of different objects. The object I am working with is as follows: let myObject = { internalValue:{city:"Paris", country:"France", pin ...

The function is unable to accurately retrieve the state value

In my app, I have a component where I'm attempting to incorporate infinite scroll functionality based on a tutorial found here. export const MainJobs = () => { const [items, setItems] = useState([]); const [ind, setInd] = useState(1); const ...

Removing the previous value in React by shifting the cursor position - a step-by-step guide

I have successfully created a phone using React that saves the numbers in an input field whether you press the keys or use the keyboard. Although the phone is functioning well, I am facing an issue where pressing the delete button or backspace key always ...

Ways to display a JSON object in CSV format

Is there a way to export a JSON object to a CSV file, where the sub-fields contain arrays of objects? I am unsure how to properly represent this embedded data in the CSV format. ...