我有一个内置在 AngularJS 中的计算器,所有字段都运行良好,除了 Select/Options 字段使结果不起作用。这是小提琴:http: //jsfiddle.net/itamark/4kkCa/
当我尝试将“颜色”放入 $scope.normalize 函数 _ref 时,它会破坏结果。当我把它拿出来时,结果很好。
这也是代码 - 因为我认为它在小提琴中不起作用:
HTML:
<!DOCTYPE html>
<html lang="en" ng-app>
<head>
<meta charset="utf-8">
<title>Calculator</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="/calc/angular.js"></script>
<script type="text/javascript" src="/calc/calculator.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="span4 offset1">
<div id="calculator" ng-controller="Calculator">
<form class="form-horizontal">
<!-- Buffer for Growth -->
<div class="control-group">
<label class="control-label">Buffer for Growth (%)</label>
<div class="controls">
<input type="text" name="growthbuffer" ng-model="buffer_for_growth" ng-change="recalculate()">
</div>
</div>
<!-- Number of Cameras -->
<div class="control-group">
<label class="control-label">Number of Cameras</label>
<div class="controls">
<input type="text" name="cameras" ng-model="camera_count" ng-change="recalculate()">
</div>
</div>
<!-- FPS -->
<div class="control-group">
<label class="control-label">FPS</label>
<div class="controls">
<input type="text" name="fps" ng:format="number" ng-model="fps" ng-change="recalculate()">
</div>
</div>
<!-- Bitrate -->
<div class="control-group">
<label class="control-label">Bitrate</label>
<div class="controls">
<input type="text" name="bitrate" ng:format="number" ng-model="bitrate" ng-change="recalculate()">
</div>
</div>
<!-- Recorded Hours -->
<div class="control-group">
<label class="control-label">Recorded Hours</label>
<div class="controls">
<input type="text" name="recordedhours" ng:format="number" ng-model="recorded_hours" ng-change="recalculate()">
</div>
</div>
<!-- Motion Percentage -->
<div class="control-group">
<label class="control-label">Motion Percentage</label>
<div class="controls">
<input type="text" name="motion_percentage" ng:format="number" ng-model="motion_percentage" ng-change="recalculate()">
</div>
</div>
<!-- Days Retained -->
<div class="control-group">
<label class="control-label">Retention (days)</label>
<div class="controls">
<input type="text" name="daysretained" ng-model="days_retained" ng-change="recalculate()">
</div>
</div>
<!-- Compression -->
<div class="control-group">
<label class="control-label">Compression</label>
<div class="controls">
<select ng-model="color" ng-options="c.name for c in colors"></select><br>
</select>
<!--<select name="compression" ng-model="video_compression" ng-change="recalculate()">
<option value="MJPEG">MJPEG</option>
<option value="MPEG4">MPEG4</option>
<option value="H.264">H.264</option>
</select>-->
</div>
</div>
</form>
<div id="display">
Bandwidth:<br>
{{ bandwidth|number:0 }} Mbps <span class="percent">
<br><br><br><br>
Live Storage:<br>
{{ livestorage|number:2 }} GB <span class="percent">
<br><br><br><br>
Storage:<br>{{ earnings_percent|number:2 }}%</span>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
JS:
// Generated by CoffeeScript 1.6.1
(function() {
window.Calculator = function($scope) {
$scope.earnings_dollar = 0.00;
$scope.earnings_percent = 0.00;
$scope.camera_count = "42";
$scope.bitrate = "2000";
$scope.fps = "10";
$scope.recorded_hours = "24";
$scope.motion_percentage = "30";
$scope.days_retained = "30";
$scope.number_days_recorded = "4.95";
$scope.colors = [
{name:'mjpeg', shade:'dark'},
{name:'mpeg4', shade:'light'},
{name:'h264', shade:'dark'}
];
$scope.normalize = function() {
var control_group, num, out, prop, val, _i, _len, _ref;
out = {};
_ref = ['camera_count', 'fps', 'number_days_recorded', 'bitrate', 'recorded_hours', 'motion_percentage'];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
prop = _ref[_i];
val = $scope[prop].replace("$", "");
num = parseFloat(val);
control_group = $("input[name='" + prop + "']").parent();
if (isNaN(num)) {
control_group.addClass("error");
return null;
} else {
control_group.removeClass("error");
out[prop] = num;
}
}
return out;
};
$scope.recalculate = function() {
var cost_basis, initial_camera_value, proceeds, props, trade_return;
props = $scope.normalize();
if (!props) {
return;
}
//bandwidth
storagekbs = props.fps * props.bitrate;
storagekbs = props.fps * props.bitrate;
ultstorage = props.camera_count * storagekbs;
bandwidth = ultstorage/1000;
$scope.bandwidth = bandwidth;
//live storage
storagekbithr = storagekbs*3600;
grossstorkbday = (storagekbithr * props.recorded_hours)/8;
grossstorgbday = grossstorkbday/1000000000;
motiongbcam = grossstorgbday * props.motion_percentage;
netgbday = motiongbcam * props.camera_count;
$scope.livestorage = netgbday * (props.recorded_hours / 24);
};
return $scope.recalculate();
};
}).call(this);