-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.html
136 lines (127 loc) · 5.72 KB
/
angular.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>MyPets</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="example.css" />
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/demock/demock.js"></script>
<script src="bower_components/demock-angular/demock-angular.js"></script>
<script>
angular
.module('example', [])
.config(demock.init())
.controller('Example', [ '$scope', '$http', '$location', function ($scope, $http, $location) {
$scope.credentials = {
username: 'john',
password: 'password'
};
function setSignedIn() {
$scope.signedIn = true;
$scope.userInfo = null;
$scope.httpStatus = null;
$http.get('api/userInfo').then(function (response) {
$scope.userInfo = response.data;
});
$http.get('api/pets').then(function (response) {
$scope.pets = response.data;
});
}
if (/signedIn/.test($location.hash())) {
setSignedIn();
}
$scope.signIn = function () {
$scope.error = null;
$scope.pending = true;
$http.post('api/signin', $scope.credentials)
.then(setSignedIn, function (response) {
var reason = response.data && response.data.error || 'Error';
$scope.error = 'Authentication failed: ' + reason;
$scope.httpStatus = { code: response.status };
})
.finally(function () {
$scope.pending = null;
});
};
$scope.signOut = function () {
$scope.signedIn = false;
$scope.userInfo = null;
$scope.pets = null;
$scope.pet = null;
};
$scope.showPet = function (idx) {
$http.get('api/pets/' + $scope.pets[idx].id).then(function (response) {
$scope.pet = response.data;
});
}
}]);
</script>
</head>
<body ng-app="example" ng-controller="Example">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">MyPets</a>
</div>
<form id="signout" class="navbar-form navbar-right" ng-show="signedIn" ng-submit="signOut()">
<button class="btn btn-default">Sign Out</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#" id="identity" ng-show="userInfo">{{ userInfo.firstName + ' ' + userInfo.lastName }}</a></li>
</ul>
</nav>
<div class="row">
<div class="col-xs-4 col-xs-offset-4">
<form id="signin" class="well well-lg" ng-show="!signedIn" ng-submit="signIn()" ng-class="{ '-pending': pending }">
<div id="error" class="alert alert-danger" ng-show="error">{{ error }}</div>
<div class="form-group">
<label for="username">Username</label>
<input id="username" name="username" ng-model="credentials.username" class="form-control" ng-disabled="pending" />
<p class="help-block">Also try: <strong>delay</strong>, <strong>timeout</strong></p>
</div>
<div class="form-group">
<label for="password">Password</label>
<input id="password" type="password" ng-model="credentials.password" class="form-control" ng-disabled="pending" />
<p class="help-block">
The correct password for username <strong>john</strong> is <strong>password</strong>
</p>
</div>
<button class="btn btn-primary btn-lg btn-block" ng-disabled="pending">Sign In</button>
</form>
<p id="http-status" class="text-muted" ng-show="httpStatus">HTTP {{ httpStatus.code }}</p>
<div id="welcome" class="alert alert-success" ng-show="{{ signedIn && userInfo }}">
<strong>Welcome {{ userInfo.firstName }}!</strong> You have successfully signed in.
</div>
<div id="pets" class="panel panel-default" ng-show="signedIn">
<div class="panel-heading">
<h3 class="panel-title">Your Pets</h3>
</div>
<div class="panel-body text-muted">
Select a pet to see its details.
</div>
<div class="panel-footer" ng-if="!pets">Loading...</div>
<table class="table table-hover" ng-if="pets">
<thead>
<tr><th>Name</th><th>Kind</th></tr>
</thead>
<tbody>
<tr ng-repeat="pet in pets"><td><a href="" ng-click="showPet($index)">{{ pet.name }}</a></td><td>{{ pet.kind }}</td></tr>
</tbody>
</table>
</div>
<div id="pet" class="panel panel-default" ng-show="pet">
<div class="panel-heading">
<h3 class="panel-title">Pet Details</h3>
</div>
<div class="panel-body">
<h2>{{ pet.name }}</h2>
<p class="text-muted">({{ pet.kind }})</p>
<hr />
<p>{{ pet.likes }}</p>
</div>
</div>
</div>
</div>
</body>
</html>