-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.html
49 lines (41 loc) · 1.34 KB
/
cart.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!-- left off at page 32//-->
<html ng-app>
<body>
<div ng-controller="CartController">
<div ng-repeat="item in items">
<span ng-bind="item.title"></span>
<input ng-model="item.quantity">
<span ng-bind="item.price | currency"></span>
<span ng-bind="item.price * item.quantity | currency"></span>
</div>
<div>Total: {{totalCart() | currency}}</div>
<div>Discount: {{bill.discount | currency}}</div>
<div>Subtotal: {{subtotal() | currency}}</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
<script type="text/javascript">
function CartController ($scope) {
$scope.bill = {};
$scope.items = [
{title: 'paint pots', quantity: 8, price: 3.95},
{title: 'polka dots', quantity: 17, price: 12.95},
{title: 'pebbles', quantity: 5, price: 6.95}
];
$scope.totalCart = function () {
var total = 0;
for (var i = 0, len = $scope.items.length; i < len; i++) {
total = total + $scope.items[i].price * $scope.items[i].quantity;
}
return total;
}
$scope.subtotal = function () {
return $scope.totalCart() - $scope.bill.discount;
};
function calculateDiscount (newValue, oldValue, scope) {
$scope.bill.discount = newValue > 100 ? 10 : 0;
}
$scope.$watch($scope.totalCart, calculateDiscount);
}
</script>
</body>
</html>