Created
August 29, 2014 00:45
-
-
Save suissa/4bbedd8bb09e53a68d46 to your computer and use it in GitHub Desktop.
Exercício de Angular consultando a API do Redtub via $http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html data-ng-app="workshopBeMEAN"> | |
<title>{{ workshop }}</title> | |
<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 --> | |
<ul> | |
<!-- instanciamos o valor de cada cerveja em beer --> | |
<li data-ng-repeat='beer in cervejas | orderBy: predicate:reverse'> | |
<!-- recuperamos os valores de name e price de cada cerveja --> | |
{{ beer.name }} - {{ beer.price }} | |
</li> | |
</ul> | |
</div> | |
<div data-ng-controller='RedtubeController'> | |
<button data-ng-click='rodar()'>Mostrar</button> | |
{{ videos }} | |
<!-- Colocar a listagem dos videos com suas thumbnails e link direto --> | |
<div data-ng-show='mostraListagem'> | |
</div> | |
</div> | |
<script src="angular.min.js"></script> | |
<script> | |
angular.module('workshopBeMEAN', ['workshopFilters', 'workshopControllers']) | |
angular.module('workshopControllers', []) | |
.controller('RedtubeController', ['$scope', '$http', | |
function($scope, $http){ | |
$scope.mostraListagem = false; | |
$scope.rodar = function(){ | |
$scope.mostraListagem = !$scope.mostraListagem; | |
} | |
// var url = 'http://cep.correiocontrol.com.br/02011200.json'; | |
var query = 'Sasha%20Gray'; | |
var url = 'http://cors-server.getup.io/url/api.redtube.com/?data=redtube.Videos.searchVideos&search='+query; | |
delete $http.defaults.headers.common['X-Requested-With']; | |
$http.get(url) | |
.success(function(data) { //função executada após o sucesso da requisição | |
console.log(data); | |
$scope.videos = data.videos; | |
}) | |
.error(function(err){ //função executada após o erro da requisição | |
console.log('Error: ', err) | |
}); | |
}]) | |
.controller('BeerController', ['$scope', '$http', | |
function($scope, $http){ | |
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]; | |
}]); | |
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> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment