-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lesson4.html
77 lines (64 loc) · 1.95 KB
/
Lesson4.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html ng-app="myApp">
<!-- We are creating an Angular Module called 'myApp' -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<title>JS Bin</title>
<script>
angular.module('myApp', []).controller('MyController', myController);
function myController($scope) {
$scope.name = 'James';
$scope.number = 0;
$scope.increment = function(amount) {
$scope.number = $scope.number + amount;
}
$scope.decrement = function() {
$scope.number = $scope.number - 1;
}
}
</script>
</head>
<body>
<div ng-controller="MyController">
Hello, my name is {{ name }}!
<p> {{number}} </p>
<p><button ng-click="increment(1)">+1</button>
<button ng-click="decrement()">-1</button></p>
</div>
</body>
</html>
<!-- When using more than one controller, best practices include the following -->
<!DOCTYPE html>
<html ng-app="myApp">
<!-- We are creating an Angular Module called 'myApp' -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script>
angular.module('myApp', [])
.controller('MyController', myController)
.controller('MySecondController', mySecondController)
function myController() {
// vm stands for view model
var vm = this;
vm.name = 'James';
}
function mySecondController() {
var vm = this;
vm.name = 'Tom';
}
</script>
</head>
<body>
<div ng-controller="MyController as MyCtrl">
{{ MyCtrl.name }}
<div ng-controller="MySecondController as MySecondCtrl">
{{ MyCtrl.name }} or {{ MySecondCtrl.name }}
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
</body>
</html>