如何在角度js中求和2 var [英] How to sum 2 var in angular js

查看:104
本文介绍了如何在角度js中求和2 var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,你好,我希望你们都好,我需要在我的角度项目中总结2个值,但我是新手,只是一个初学者杠杆角程序员我不知道如何使角度变量并总结它



我的尝试:



< TBODY> 
var num1,num2,num3
num1 = item.item_qty;
num2 = item.product_price
num3 = num1 + num2;
< tr ng-repeat =订单中的商品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>

解决方案

我认为你最好不要加/减您的控制器中的角度变量而不是视图。



您应该为项目对象添加另一个属性...我将其称为sumOfTwoVars。所以你应该在你的控制器中有这样的东西(注意,你没有包含控制器代码所以我不知道你的代码是什么样的。)



< pre lang =javascript> var item = {};
item.sumOfTwoVars = item.item_qty + item.product_price;





然后在您的视图中,而不是 {{num3}} 然后您可以执行 {{item.sumOfTwoVars}} ,这会阻止您的视图执行任何操作/逻辑从控制器渲染数据。


你可以试试这种方式



创建一个文件夹并放置HTML和Controller文件一样的地方。我在桌面上创建了文件夹,然后首先创建HTML文件,然后在桌面文件夹中创建一个Controller文件。



步骤1:创建 index.html 文件。



打开记事本并粘贴到代码下方,我将其保存为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 =myAppng-controller =testCtrl>
< table border =1>
< tr ng-repeat =订单中的商品>
< td> {{item.item_qty}}< / td>
< td> {{item.product_name}}< / td>
< td> {{item.status}}< / td>
< td> {{item.product_price}}< / TD>
< td> 总和: {{item.item_qty + item.product_price}}< / td>
< / tr>
< / table>

< / div>

< / html>

第2步:创建 testController.js 文件。

打开记事本并粘贴到下面代码并将文件另存为testController.js:



 angular.module('myApp',[])。controller('testCtrl' ,function(


scope){


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>

解决方案

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).

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.


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) {


这篇关于如何在角度js中求和2 var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆