我对Angular很新。我有两个正在运行的计算器,我尝试添加两个$scope
值GrandTotal
和GrandTotalTwo
,以获得两个总和TotalSum
。
<div ng-app="myApp" ng-controller="myController">
<table>
<tr>
<th>Camp Fees</th>
<th>Number of Campers</th>
<th>Rates</th>
<th>Totals</th>
</tr>
<tr ng-repeat="event in events">
<td>{{event.name}}</td>
<td><input placeholder="0" class="qty" type="number"
ng-model="quantity" min="0"
ng-change="recalc(event, quantity )"
restrict-to="[0-9]" />
</div>
</td>
<td>{{event.cost | currency : $}}</td>
<td class="subTotal" placeholder="$0.00">
{{ quantity*event.cost | currency : $}}
</td>
</tr>
</table>
<div>
<h2>Camp Fee Total : {{GrandTotal | currency : $}}</h2>
</div>
<br>
<div ng-controller="myControllerTwo">
<table>
<tr>
<th>Accommodation Fees</th>
<th>Rooms Needed</th>
<th>Rates</th>
<th>Totals</th>
</tr>
<tr ng-repeat="eventTwo in eventsTwo">
<td>{{eventTwo.nameTwo}}</td>
<td><input placeholder="0" class="qty" type="number"
ng-model="quantityTwo" min="0"
ng-change="recalcTwo(eventTwo, quantityTwo )"
restrict-to="[0-9]" />
</td>
<td>{{eventTwo.costTwo | currency : $}}</td>
<td class="subTotal">{{ quantityTwo*eventTwo.costTwo | currency : $}}</td>
</tr>
</table>
<div>
<h2>Accommodation Fee Total : {{GrandTotalTwo | currency : $}}</h2>
</div>
</div>
</div>
<div ng-controller="ToAddTotal">
<h2>Sum = {{TotalSum | currency : $}}</h2>
</div>
(function() {
var myApp = angular.module("myApp", []);
myApp.controller("myController", ["$scope", myController]);
var events = [{
name: "Adults",
cost: 485,
itemTotal: 0
}, {
name: "Kids 13-18",
cost: 394,
itemTotal: 0
}, {
name: "Kids 4-12",
cost: 307,
itemTotal: 0
}, {
name: "Kids 0-3",
cost: 100,
itemTotal: 0
}];
function myController($scope) {
$scope.events = events;
$scope.recalc = function(item, quantity) {
item.itemTotal = quantity * item.cost;
$scope.GrandTotal = 0;
var sum = 0;
angular.forEach($scope.events, function(event) {
$scope.GrandTotal = $scope.GrandTotal + event.itemTotal;
$scope.GrandTotal == NaN ? "custom msg" : $scope.GrandTotal;
// console.log($scope.GrandTotal);
})
};
}
myApp.controller("myControllerTwo", ["$scope", myControllerTwo]);
var eventsTwo = [{
nameTwo: "Hotel Style Rooms:",
costTwo: 535, //Not relevant info (display: none in css - line:745)
itemTotalTwo: 0 //Not relevant info (display: none in css - line:745)
}, {
nameTwo: "First Room",
costTwo: 535,
itemTotalTwo: 0
}, {
nameTwo: "Additional Room(s)",
costTwo: 445,
itemTotalTwo: 0
}, {
nameTwo: "RV Sites",
costTwo: 175,
itemTotalTwo: 0
}];
function myControllerTwo($scope) {
$scope.eventsTwo = eventsTwo;
$scope.recalcTwo = function(item, quantityTwo) {
item.itemTotalTwo = quantityTwo * item.costTwo;
$scope.GrandTotalTwo = 0;
var sumTwo = 0;
angular.forEach($scope.eventsTwo, function(eventTwo) {
$scope.GrandTotalTwo = $scope.GrandTotalTwo + eventTwo.itemTotalTwo;
$scope.GrandTotalTwo == NaN ? "custom msg" : $scope.GrandTotalTwo;
// console.log($scope.GrandTotal);
})
};
}
function ToAddTotal($scope) {
$scope.TotalSum = function() {
return $scope.GrandTotal * 1 + $scope.GrandTotalTwo * 1;
};
}
})();
答案 0 :(得分:1)
您可以使用ng-change
和ng-model
指令执行所有操作 - 无需自定义代码:
ng-change
属性以设置总数: [为了清晰起见简化] ng-change="$parent.TotalSum = $parent.GrandTotal + $parent.GrandTotalTwo"
ng-model
属性设置为TotalSum
答案 1 :(得分:0)
原来我注意到我有一个额外的</div>
,它丢掉了我的代码。
我删除了:
function ToAddTotal($scope) {
$scope.TotalSum = function() {
return $scope.GrandTotal * 1 + $scope.GrandTotalTwo * 1;
};
}
和
<div ng-controller="ToAddTotal">
<h2>Sum = {{TotalSum | currency : $}}</h2>
</div>
而是在HTML中写了这个:
<h2>Total Sum : {{GrandTotal*1 + GrandTotalTwo*1 | currency : $}}</h2>
如果有人有兴趣查看所有代码,我的最终产品是here。