如何在resolve方法中传递查询参数

时间:2014-04-28 15:14:03

标签: angularjs angularjs-ng-route

这是我的js代码

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?
                }
            }
        });
}

我想在加载视图之前通过ajax调用获取用户,如何解析id参数?

1 个答案:

答案 0 :(得分:2)

注入$route然后:

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