ch04-04-two-forms-databinding.html
1.3 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
<!DOCTYPE html>
<html ng-app="notesApp">
<head>
<title>Notes App</title>
<script src="../../tp/angular.js"></script>
<style>
span {
background-color: #cce;
}
</style>
</head>
<body ng-controller="MainCtrl as ctrl">
<form ng-submit="ctrl.submit1()">
<input type="text" placeholder="username" ng-model="ctrl.username"/>
<input type="password" placeholder="password" ng-model="ctrl.password"/>
<input type="submit" value="Submit"/>
</form>
<!-- Better way of structuring the form data -->
<form ng-submit="ctrl.submit2()">
<input type="text" placeholder="username" ng-model="ctrl.user.username"/>
<input type="password" placeholder="password" ng-model="ctrl.user.password"/>
<input type="submit" value="Submit"/>
</form>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function () {
var self = this;
self.submit1 = function () {
// create user object to send to server
var user = {username: self.username, password: self.password};
console.log('First submit: ', user);
};
self.submit2 = function () {
console.log('Second submit: ', self.user);
};
}]);
</script>
</body>
</html>