exam/index.html

90 lines
2.3 KiB
HTML
Raw Normal View History

2017-05-19 14:53:27 +03:00
<!doctype html>
2017-06-07 14:55:51 +03:00
<html lang="et" ng-app="app">
2017-05-19 14:53:27 +03:00
<head>
<meta charset="utf-8">
</head>
2017-06-07 11:47:33 +03:00
<body ng-controller="C1 as vm">
2017-05-19 14:53:27 +03:00
2017-06-07 14:55:51 +03:00
<div ng-repeat="item in vm.cart">
{{ item.name }}
<a href="" style="text-decoration: none" ng-click="vm.changeCount(item, -1)">&nbsp;-&nbsp;</a>
<input size="1" ng-model="item.count" ng-change="vm.setCount(item)" ng-reflect-model="1">
<a href="" style="text-decoration: none" ng-click="vm.changeCount(item, +1)">&nbsp;+&nbsp;</a>
</div>
<br>
Kokku: {{ vm.total }} EUR
<br><br>
<button ng-click=vm.submitOrder();>Saada tellimus</button>
2017-05-19 14:53:27 +03:00
2017-05-28 09:37:05 +03:00
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
2017-05-19 14:53:27 +03:00
<script>
2017-06-07 14:55:51 +03:00
(function () {
'use strict';
angular.module('app', []);
angular.module('app').controller('C1', function ($http) {
var vm = this;
vm.cart = [
{
"_id":0,
"name":"Banaanid (1 EUR/KG)",
"price":1,
"count":0
},
{
"_id":1,
"name":"Õunad (2 EUR/KG)",
"price":2,
"count":0
}
];
vm.total = 0;
vm.calcTotal = calcTotal;
vm.changeCount = changeCount;
vm.setCount = setCount;
vm.submitOrder = submitOrder;
calcTotal();
function calcTotal(){
var total = 0;
for (let item of vm.cart) {
total += item.price*item.count;
}
vm.total = total;
}
function changeCount(item, amount) {
var count = parseInt(item.count, 10) + amount;
if (count >= 0) {
item.count = count;
}
calcTotal();
}
function setCount(item, amount) {
// How to apply changes?
var count = parseInt(item.count, 10);
if (count == NaN) {
count = 0;
}
item.count = count;
2017-06-10 17:25:10 +03:00
calcTotal();
2017-06-07 14:55:51 +03:00
}
function submitOrder(){
$http.post("/api/cart/order", vm.cart)
.then(resp => console.log("Cart submitted", resp))
.catch(error => console.log("Submittion failed", error))
}
});
2017-05-19 14:53:27 +03:00
2017-06-07 14:55:51 +03:00
})();
2017-05-19 14:53:27 +03:00
</script>
</body>
</html>