Skip to content

Instantly share code, notes, and snippets.

@Vp3n
Created January 31, 2014 22:08
Show Gist options
  • Save Vp3n/8744248 to your computer and use it in GitHub Desktop.
Save Vp3n/8744248 to your computer and use it in GitHub Desktop.
AngularJS: route resolve
angular.module('App', ['ngRoute'])
.run(function(repositories) {
//ensure that request is sent on application start, you could also
//call load() on route.resolve() depending on your needs
repositories.load();
})
.config(function($routeProvider) {
$routeProvider
.when('/', {
resolve: {
// route will wait for promise to be resolved before render
github: function(repositories) {
//could be: return repositories.load()
return repositories.promise;
}
}
})
});
angular.module('App').service('repositories', function($http) {
var promise;
return {
load: function() {
promise = $http.get('https://api.github.com/search/repositories?q=angular');
return promise;
},
promise: function() {
return promise;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment