angularjs使用$ scope减去

时间:2016-01-28 14:30:27

标签: javascript angularjs scope

我非常陌生,不仅要求帮助,还要使用Angular,所以请耐心等待。

下面是我用来为灯泡创建能量刻度的我的calculator.js代码的副本 - 我目前停留在我想要显示可以对其他灯泡节省多少的点上,例如....

卤素总量 - 白炽总量
hal_total - inc_total

有人请你好好看看我的代码,并指出我的方向正确。

    /* 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();

    }]);    

    })();

2 个答案:

答案 0 :(得分:0)

以此plnkr为例:http://plnkr.co/edit/9J3CIyoC06k45R0JJR4i?p=preview

您可以显示计算差异的一种方法是在模板中内联计算:

{{inc_total - hal_total}}
在进行内联计算时,另一个有用的部分是利用角度数字滤波器https://docs.angularjs.org/api/ng/filter/number;此代码将显示带有两位小数的计算数字:

{{inc_total - hal_total | number:2}} 

答案 1 :(得分:0)

好的,所以Cameron首先给了我答案,然后Paul非常友好地提供了小数位代码,简单的答案是在div中添加以下代码(或者在我的情况下是段落)....

{{inc_total - hal_total |号码:2}}