简体   繁体   English

使用Angular计算总和

[英]Calculating sum using Angular

I'm beginner on AngularJs and I have a difficult to show the sum of two numbers. 我是AngularJs的初学者,我很难显示两个数字的总和。 The first script work perfectly : 第一个脚本完美地工作:

<div>
   <p><input type="text" ng-model="price1" /> <strong>{{price1}}</strong></p>
   <p><input type="text" ng-model="price2" /> <strong>{{price2}}</strong></p>
   <p><strong>{{total = (price1 * 1 + price2 * 1) }}</strong></p>
</div>

But when I try to test this using a controller, like this : 但是当我尝试使用控制器测试时,如下所示:

<div ng-controller="myCtrl">
   <p><input type="text" ng-model="price1" /> <strong>{{price1}}</strong></p>
   <p><input type="text" ng-model="price2" /> <strong>{{price2}}</strong></p>
   <p><strong>{{total}}</strong></p>
</div>
<script>
   var app = angular.module('firstApp', []);
   app.controller('myCtrl', ['$scope', function($scope) {
      $scope.total = ($scope.price1  + $scope.price2);
   }]);
</script>

I have no result, just a NaN value. 我没有结果,只有一个NaN值。 I have no idea what happen ! 我不知道发生了什么!

There are several issues in your code: 您的代码中存在几个问题:

  1. The variables $scope.price1 and $scope.price2 are not defined once the controller is initialized. 初始化控制器后,未定义变量$scope.price1$scope.price2 Arithmetical operation using undefined value produces NaN as the result. 使用undefined值的算术运算会产生NaN作为结果。
  2. You compute the $scope.total just once. 您只需计算一次$scope.total Its result is not updated on change of $scope.price1 or $scope.price2 其结果未在$scope.price1$scope.price2更改时更新
  3. In the inputs you should use type="number" not type="text" then the values of $scope.price1 and $scope.price2 will be parsed and summed up as numbers. 在输入中,您应该使用type="number"而不是type="text"然后将解析$scope.price1$scope.price2的值并将其汇总为数字。 In your code they are concatenated as strings. 在您的代码中,它们被串联为字符串。

 var app = angular.module('myApp', []); app.controller("myCtrl", function ($scope) { $scope.price1 = 1; $scope.price2 = 2; $scope.getTotal = function() { return $scope.price1 + $scope.price2; }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="myCtrl"> <p><input type="number" ng-model="price1" /> <strong>{{price1}}</strong></p> <p><input type="number" ng-model="price2" /> <strong>{{price2}}</strong></p> <p><strong>{{getTotal()}}</strong></p> </div> </div> 

Do you need to set default values? 你需要设置默认值吗? Is it saying NaN because you haven't entered anything in the input boxes? 它是说NaN,因为你没有在输入框中输入任何东西?

Try initializing the variables in the controller above the total calculation. 尝试在总计算上方初始化控制器中的变量。

$scope.price1 = 0;
$scope.price2 = 0;

The values are undefined when you call the sum, just give them an initial value 调用sum时,值是未定义的,只需给它们一个初始值

$scope.price1 = 1;
$scope.price2 = 2;
$scope.total = ($scope.price1  + $scope.price2);

What you are doing is equals to: 你在做什么等于:

$scope.total = ($scope.price1  + $scope.price2);
$scope.total = (undefined  + undefined); //equals to NaN

I do it but I have to give a value in my controller for price1 and price2, like that : 我这样做但是我必须在我的控制器中给出price1和price2的值,就像那样:

<script>
   var app = angular.module('firstApp', []);
   app.controller('myCtrl', ['$scope', function($scope) {
      $scope.price1 = 1;
      $scope.price2 = 2;
      $scope.total = ($scope.price1 + $scope.price2);
   }]);
</script>

But I have two input. 但我有两个输入。 I want to use them like a calculator, put a value into them and see the result. 我想像计算器一样使用它们,将值放入其中并查看结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM