forked from zafarali/learning-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00-1-concepts.html
More file actions
56 lines (56 loc) · 1.85 KB
/
00-1-concepts.html
File metadata and controls
56 lines (56 loc) · 1.85 KB
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
51
52
53
54
55
56
<!DOCTYPE html>
<html>
<head>
<title>00-concepts</title>
<!--This script contains all the code which makes AngularJS Run on our website-->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.min.js"></script>
<script>
angular.module('invoice-cntrl-demo', [])
//.controller is a function that creates a controller using a function we supply.
.controller('InvoiceController', function(){
//all standard JS stuff when creating an object
this.qty=1;
this.cost=2;
this.selectedCurrency='USD';
this.currencies = ['USD', 'EUR', 'CNY'];
this.usdExchangeRates = {
USD:1, EUR:0.74, CNY:6.09
};
this.total = function total(outputCurrency){
return this.convert(this.qty*this.cost, this.selectedCurrency, outputCurrency);
};
this.convert = function convert(amount, from, to){
return amount * this.usdExchangeRates[to] / this.usdExchangeRates[from];
};
this.pay = function pay(){
window.alert("thanks!");
};
});
</script>
</head>
<body>
<h1>Controllers</h1><br />
<div ng-app="invoice-cntrl-demo" ng-controller="InvoiceController as invoice">
<!--Says that the 'InvoiceController' is responsible for the element and we store it into 'invoice' on the scope-->
<b>Invoice:</b>
<div>
Quantitiy: <input type="number" ng-model="invoice.qty" required>
</div>
<div>
Costs: <input type="number" ng-model="invoice.cost" required>
<select ng-model="invoice.selectedCurrency">
<option ng-repeat="c in invoice.currencies">{{c}}</option>
<!--ng-repeat iterates through all elements in the currencies attribute of the invoice object-->
</select>
</div>
<div>
<b>Total:</b>
<span ng-repeat="c in invoice.currencies">
{{invoice.total(c) | currency:c }}
</span>
<button class="btn" ng-click="invoice.pay()">Pay</button>
<!--Refers to the pay() function within the invoice object-->
</div>
</div>
</body>
</html>