-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex010-refatored.html
136 lines (120 loc) · 4 KB
/
ex010-refatored.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
<!doctype html>
<html data-ng-app="workshopBeMEAN">
<head>
<title>{{ workshop }}</title>
<style>
.user-avatar {
width: 200px;
}
.user-label {
font-weight: bold;
}
</style>
</head>
<body>
<div data-ng-controller='BeerController'>
<!-- Exibição do array de cervejas setado no BeerController -->
{{ cervejas }}
<pre>Sorting predicate = {{predicate}}; reverse = {{reverse}}</pre>
<a href="" data-ng-click="predicate = 'name'; reverse=!reverse">Nome</a>
<!-- Listamos as cervejas do nosso escopo a diretiva ng-repeat -->
<table>
<thead>
<tr>
<th data-ng-click='ordenar("name")'>Nome</th>
<th data-ng-click='ordenar("price")'>Preço</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat='beer in cervejas | orderBy:predicate:reverse'>
<td>{{ beer.name }}</td>
<td>{{ beer.price }}</td>
</tr>
</tbody>
</table>
</div>
<div data-ng-controller='UserController'>
<button data-ng-click='rodar()'>Usuario</button>
<p data-ng-show='mostraUser'>
<img class='user-avatar' data-ng-src="{{user.avatar_url}}" alt="">
<br />
<span class='user-label'>Login:</span> {{user.login}}
<br />
<span class='user-label'>Name:</span> {{user.name}}
<br />
<span class='user-label'>Company:</span> {{user.company}}
<br />
<span class='user-label'>Blog:</span> {{user.blog}}
<br />
<span class='user-label'>Email:</span> {{user.email}}
<br />
<span class='user-label'>Location:</span> {{user.location}}
</p>
</div>
<script src="angular.min.js"></script>
<script>
angular.module('workshopBeMEAN', ['workshopFilters', 'workshopControllers'])
// Criando módulod e controllers
angular.module('workshopControllers', [])
.controller('UserController', UserController)
.controller('BeerController', BeerController);
function BeerController($scope) {
$scope.reverse = true;
$scope.predicate = 'name';
$scope.ordenar = function(predicado){
$scope.reverse = !$scope.reverse;
$scope.predicate = predicado;
}
var cerveja1 = {name: 'kaiser', price: 2};
var cerveja2 = {name: 'skol', price: 3};
var cerveja3 = {name: 'glacial', price: 4};
var cerveja4 = {name: 'polar', price: 6};
// ADICIONANDO AS CERVEJAS NO SCOPE DO CONTROLLER
$scope.cervejas = [cerveja1, cerveja2, cerveja3, cerveja4];
}
function UserController($scope, $http) {
$scope.mostraUser = false;
// exemplo de função que irá rodar com um CLICK
$scope.rodar = function(){
$scope.mostraUser = !$scope.mostraUser;
}
var url = 'https://api.github.com/users/suissa';
var method = 'GET';
$http({
url: url,
method: method
}).
success(function(data){
console.log('Data: ', data);
$scope.user = data;
}).
error(function(err){
console.log('Erro: ', err);
});
}
BeerController['$inject'] = ['$scope'];
UserController['$inject'] = ['$scope', '$http'];
angular.module('workshopFilters', [])
.filter('reverseName', function () {
return function (text) {
if(text)
return text.split("").reverse().join("");
};
})
.filter('truncate', function () {
return function (text, length, end) {
if (isNaN(length))
length = 10;
if (end === undefined)
end = "...";
if (text.length <= length || text.length - end.length <= length) {
return text;
}
else {
return String(text).substring(0, length-end.length) + end;
}
};
});
</script>
</body>
</html>