coure2011 coure2011 - 2 months ago 14
AngularJS Question

how to pass query parameter in resolve method

Here is my js code

var MYApp = angular.module('myApp', ['ngRoute', 'myAppServices', 'ngSanitize'])
.config(myRouter);


angular.module('myAppServices', ['ngResource'])
.factory('GridsAPI', function($resource) {
return {
GetUser: $resource('/MY/system/getUser/:id')
};
});
function ViewUserController($scope, user) {

}
function myRouter($routeProvider) {

$routeProvider
.when('/users/view/:id', {
templateUrl: '/MY/system/users/view/:id',
controller: 'ViewUserController',
resolve: {
user: function(GridsAPI) {
//how to return user based on id passed to this url?
}
}
});
}


I want to get the user via ajax call before the view has loaded, how can I get the id parameter in resolve?

Answer

Inject $route and then:

resolve: {
    user: ['$route', 'GridsAPI', function($route, GridsAPI) {
      var userId = $route.current.params.id;
      ...
    }]
}