<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>Parse Expression Example</title>
<link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="js/angular.min.js"></script>
<style type="text/css">
</style>
</head>
<body ng-controller="ServiceController">
<div>
<label for="username">Type in a Github username</label>
<input type="text" name="username" ng-model="username" placeholder="Enter a Github username">
<ul>
<li ng-repeat="event in events">
{{ event.actor.login }} | {{ event.repo.name }}
</li>
</ul>
</div>
</body>
<script type="text/javascript">
angular.module('myApp.services', [])
.config(function($sceDelegateProvider){
$sceDelegateProvider.resourceUrlWhitelist([
// Allow same origin resource loads.
'self',
'https://api.github.com/**'
]);
$sceDelegateProvider.resourceUrlBlacklist([
'https://www.baidu.com'
]);
})
.factory('githubService', function($http){
var githubUrl = 'https://api.github.com';
var runUserRequest = function(username, path){
return $http({
method: 'JSONP',
url: githubUrl + '/users/' + username + '/' + path + ''
});
};
return {
events: function(username){
return runUserRequest(username, 'events');
}
};
});
angular.module('myApp', ['myApp.services'])
.controller('ServiceController', function($scope, $timeout, githubService) {
var timeout;
$scope.$watch('username', function(newUsername){
if(newUsername){
if(timeout){
$timeout.cancel(timeout);
}
timeout = $timeout(function(){
githubService.events(newUsername)
.then(function(res){
if(res.data && angular.isArray(res.data.data)){
$scope.events = res.data.data;
}else {
$scope.events = [];
}
});
}, 900);
}
});
});
</script>
</html>