Created
January 31, 2014 22:08
-
-
Save Vp3n/8744248 to your computer and use it in GitHub Desktop.
AngularJS: route resolve
This file contains hidden or 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
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