agon024 agon024 - 1 year ago 70
HTML Question

Grabbing multiple JSON key values and then adding them together

Here is the code for my angular code:

angular.module('ngApp', [])
.factory('authInterceptor', authInterceptor)
.constant('API', '')
.controller('carDetails', carDetails)

function carDetails($scope, $http, API) {
$http.get( API + '/car/12208' ).
success(function(data) {
$scope.details = data;

Here is a pic of the JSON data I am working with:

enter image description here

As you can see under Financial Info I have multiple keys that have currency values in them. How can I pull out those values in angular and add them together and then display that result in my HTML file.

I can obviously get land_curr and land_prev values in the html by using

{{ details['Financial Info'].land_curr }}

But in the HTML that I have been given there is a spot for "total amount". So I need to take those values, add them together and then put that new value into the "total amount" section in the HTML file.

Is there a way I can do this?


Note that these JSON values will change so I cant "hard code" the currency values into anything as the currency amounts will change often. Also I need to be able to add all "_prev" values together and "_curr" values together. And I need to just be able to add, for instance, "land_curr" and "land_prev" together too.

Answer Source

OK. So I finally got this working. Thank you to @amirsasson for getting the ball rolling and everyone else here who gave me the info that they did. I really appreciate it.

So you can obviously subtract, divide and multiply expression values by using:

{{ details['Financial Info'].land_curr - details['Financial Info'].land_prev }}

{{ details['Financial Info'].land_curr / details['Financial Info'].land_prev }}

{{ details['Financial Info'].land_curr * details['Financial Info'].land_prev }}

But when I tried to add them as per @amirsasson solution it would not sum up the two values it would just put them side by side.

So What I had to do was multiply the values in the expression by 1 and then add them together like so:

${{ (details['Financial Info'].land_curr * 1 ) + (details['Financial Info'].land_prev * 1) }}

This worked exactly how I needed it to.

Thanks for everyones help.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download