0

这是我的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 参数?

4

1 回答 1

2

注入$route然后:

resolve: {
    user: ['$route', 'GridsAPI', function($route, GridsAPI) {
      var userId = $route.current.params.id;
      ...
    }]
}
于 2014-04-28T15:23:56.500 回答