Javascript angularjs使用$scope进行减法
我是一个非常新的不仅要求帮助,但也使用角,所以请容忍我 下面是我的calculator.js代码的副本,我正在使用它为灯泡创建一个能量刻度-我目前正处于一个点上,我想显示相对于其他灯泡可以节省多少,例如 卤素总量-白炽灯总量Javascript angularjs使用$scope进行减法,javascript,angularjs,scope,Javascript,Angularjs,Scope,我是一个非常新的不仅要求帮助,但也使用角,所以请容忍我 下面是我的calculator.js代码的副本,我正在使用它为灯泡创建一个能量刻度-我目前正处于一个点上,我想显示相对于其他灯泡可以节省多少,例如 卤素总量-白炽灯总量 hal_总计-公司总计 请有人看看我的代码,也许能给我指出正确的方向 /* JavaScript Document */ (function(){ var app = angular.module('myCalculator',[]);
hal_总计-公司总计 请有人看看我的代码,也许能给我指出正确的方向
/* JavaScript Document */
(function(){
var app = angular.module('myCalculator',[]);
app.controller('calculatorcontroller',['$scope',function($scope){
$scope.lumen_options = [375,600,900,1125,1600];
$scope.current_lumens = 600;
$scope.current_cost = 12;
$scope.current_hours = 3;
$scope.current_years = 1;
$scope.current_bulbs = 1;
$scope.total_days = 365;
$scope.inc_conversion = .0625;
$scope.hal_conversion = .0450;
$scope.cfl_conversion = .0146;
$scope.led_conversion = .0125;
$scope.calculate = function(){
$scope.inc_wattage = ($scope.current_lumens * $scope.inc_conversion).toFixed(1);
$scope.hal_wattage = ($scope.current_lumens * $scope.hal_conversion).toFixed(1);
$scope.cfl_wattage = ($scope.current_lumens * $scope.cfl_conversion).toFixed(1);
$scope.led_wattage = ($scope.current_lumens * $scope.led_conversion).toFixed(1);
if( $scope.current_hours > 24 ){ $scope.current_hours = 24 }
var total_hours = $scope.total_days * $scope.current_hours;
var total_total = $scope.total_days * $scope.current_hours * $scope.current_years * $scope.current_bulbs;
var cost = $scope.current_cost / 100;
$scope.inc_cost = ((($scope.inc_wattage * total_hours) / 1000) * cost).toFixed(2);
$scope.hal_cost = ((($scope.hal_wattage * total_hours) / 1000) * cost).toFixed(2);
$scope.cfl_cost = ((($scope.cfl_wattage * total_hours) / 1000) * cost).toFixed(2);
$scope.led_cost = ((($scope.led_wattage * total_hours) / 1000) * cost).toFixed(2);
$scope.inc_total = ((($scope.inc_wattage * total_total) / 1000) * cost).toFixed(2);
$scope.hal_total = ((($scope.hal_wattage * total_total) / 1000) * cost).toFixed(2);
$scope.cfl_total = ((($scope.cfl_wattage * total_total) / 1000) * cost).toFixed(2);
$scope.led_total = ((($scope.led_wattage * total_total) / 1000) * cost).toFixed(2);
}
$scope.calculate();
}]);
})();
以该plnkr为例: 显示计算出的差异的一种方法是在模板中内联计算:
{{inc_total - hal_total}}
在进行内联计算时,另一个有用的方法是利用angular的数字过滤器;此代码将显示带两位小数的计算数字:
{{inc_total - hal_total | number:2}}
Cameron先给了我答案,然后Paul非常友好地给出了小数位代码,简单的答案是在一个div中添加以下代码(或者在我的例子中是一个段落)
{{inc_-total-hal_-total{124; number:2}你能突出显示不起作用的代码吗?你能不能把像
{{hal_-total-inc_-total}}
这样的东西放在你的html控制器的div
中?你也能显示你的html代码吗?非常感谢你,卡梅隆和保罗-这正是我一直在寻找的,我一直在寻找最困难的解决方案,应该很容易-谢谢大家