-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.html
50 lines (48 loc) · 1.5 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
50
<html ng-app='myApp'>
<head>
<title>Your shopping cart</title>
</head>
<body ng-controller='CartController'>
<h1>Your order</h1>
<div ng-repeat='item in items'>
<span>{{item.title}}</span>
<input type="text" ng-model='item.quantity'>
<span>{{item.price}}</span>
<span>{{item.price * item.quantity}}</span>
<button ng-click="remove($index)">Remove</button>
</div>
<div>Total: {{totalCart() | currency }}</div>
<div>Discount: {{bill.discount | currency}}</div>
<div>Subtotal: {{subtotal() | currency}}</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script>
var myApp = angular.module('myApp', []);
myApp.controller('CartController', ['$scope', function ($scope) {
$scope.currency = '$';
$scope.bill = {};
$scope.items = [
{title: 'sach', quantity: 3, price: 27000},
{title: 'vo', quantity: 6, price: 8000},
{title: 'but', quantity: 11, price: 3500}
];
$scope.remove = function(index){
$scope.items.splice(index,1);
}
$scope.totalCart = function(){
var total = 0;
for (var i=0;i<$scope.items.length;i++){
total = total + $scope.items[i].price * $scope.items[i].quantity;
}
return total;
}
$scope.subtotal = function(){
return $scope.totalCart() - $scope.bill.discount;
}
function getDiscount(newValue, oldValue, scope){
$scope.bill.discount = newValue > 100 ? 10 : 0;
}
$scope.$watch($scope.totalCart, getDiscount);
}]);
</script>
</body>
</html>