Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys, how are you i hope you all be fine guys i need to sum 2 values in my angular project but am new and just a beginner lever angular programmer i dont know how to make variable in angular and sum it

What I have tried:

<tbody>
    var num1,num2,num3
num1 = item.item_qty;
num2 = item.product_price
num3 = num1 + num2;
    <tr ng-repeat="item in orders" class="animated fadeInUp">
        
      <td>{{item.item_qty}}</td>
      <td>{{item.product_name}}</td>
      <td>{{item.status}}</td>
      <td>{{item.product_price}}</td>
<pre>      <td>{{num3}}</td>
Posted
Updated 21-Jun-17 10:27am

I think you would be better off putting the adding/subtracting of your angular variables in your controller rather than your view.

You should add another property to your item object...i'll call it sumOfTwoVars. So you should have something like this in your controller (note, you didn't include controller code so I don't know what your code looks like).

JavaScript
var item = {};
item.sumOfTwoVars = item.item_qty + item.product_price;


Then in your view, instead of doing {{num3}} you can then just do {{item.sumOfTwoVars}} which prevents your view from doing any manipulation/logic to render data from the controller.
 
Share this answer
 
Comments
David Crow 21-Jun-17 15:37pm    
I'm not sure why the Android tag is part of this question. Is that intentional?
David_Wimbley 22-Jun-17 12:16pm    
I didn't do it.
Member 9983063 21-Jun-17 16:25pm    
well i have tried your code when i run my app so it's not shown sum column in my application i have first paste your code in controller.js and then i paste var name in my form where i want to show this value but it's not showing
hi you can try this way

create a folder and place HTML and Controller file at same place. I created folder on desktop, then First create HTML file then Create a Controller file inside desktop folder.

Step 1: to create index.html file.

open notepad and paste below code and I saved it as "index.html" :

<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>

<script src="testController.js"></script>
</head>

<div ng-app="myApp" ng-controller="testCtrl">
<table border="1">
<tr ng-repeat="item in orders">
<td>     {{item.item_qty}}     </td>
<td>     {{item.product_name}} </td>
<td>     {{item.status}}       </td>
<td>     {{item.product_price}}</td>
<td>	 Sum: {{item.item_qty + item.product_price}} </td>
  </tr>
</table>

</div>

</html>
Step 2: to create testController.js file.
open notepad and paste below code and save file as "testController.js" :

angular.module('myApp', []).controller('testCtrl', function($scope) {
     $scope.orders = [
         {item_qty:10,product_name:'Test1', status: '', product_price: 350},
         {item_qty:3,product_name:'Test2', status: '', product_price: 100},
         {item_qty:25,product_name:'Test3', status: '', product_price: 200}
     ];
 }); 


once both files are created then open "index.html" in browser (I used Internet Explorer). Hope this will help you..
 
Share this answer
 
v5
Comments
Member 12464509 21-Jun-17 16:46pm    
var num1,num2,num3
num1 = item.item_qty;
num2 = item.product_price
num3 = num1 + num2;


{{item.item_qty}}
{{item.product_name}}
{{item.status}}
{{item.product_price}}
      {{item.item_qty + item.product_price}} </td




Remmove below code lines and I replaced num3 with expression {{item.item_qty + item.product_price}} :
var num1,num2,num3
num1 = item.item_qty;
num2 = item.product_price
num3 = num1 + num2;
I am not sure why are you using these.....
Member 9983063 21-Jun-17 16:42pm    
Hey Thank you bro you make my day i have done it with your code Thank you so much
Member 12464509 21-Jun-17 16:49pm    
No problem!! you are welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900