AngularJS ui-router中的URL路由参数

时间:2016-02-05 15:38:52

标签: javascript angularjs angular-ui-router

我希望能够重新加载我的应用程序的嵌套视图并附加一个路由参数,以便我可以在我的应用程序中进行URL路由。我无法弄清楚如何做到这一点,我最初让它使用这样的查询:

$location.search('userId', user._id);
//http://localhost:9000/#/user/?userId=123456789

我想要的网址在下面,userId = 123456789

http://localhost:9000/#/user/123456789

我的app.js文件

$stateProvider
    .state('index', {
        url: '/',
        views: {
            '@' : {
                templateUrl: 'views/layout.html'
            },
            'top@index' : {
                templateUrl: 'views/top.html',
                controller: function($scope, $state) {
                    $scope.userLogOut = function() {
                        $state.go('login');
                    };
                }
            },
            'left@index' : { templateUrl: 'views/left.html' },
            'main@index' : { templateUrl: 'views/main.html' }
        }
    })
    .state('index.user', {
        url: 'user:userId',
        templateUrl: 'views/user/user.html',
        controller: 'UserCtrl'
    })
    .state('index.user.detail', {
        url: '/',
        views: {
            'detail@index' : {
                templateUrl: 'views/user/details.html',
                controller: 'DetailCtrl'
            }
        }
    })

在我的控制器中:

$state.reload('index.user.detail', {userId: $scope.user._id});

1 个答案:

答案 0 :(得分:0)

在使用ui-router时,您可以使用$state.go('index.user', { userId: {{yourid}} });

要允许查询字符串参数使用ui-router工作,请写下您的状态,

.state('index.user', {
    url: 'user/?userId=:param',
    templateUrl: 'views/user/user.html',
    controller: 'UserCtrl'
})

这样就可以了,

  

// http://localhost:9000/#/user/?userId=123456789

没有查询字符串参数(您想要的URL)就是这个,

.state('index.user', {
    url: 'user/:userId',
    templateUrl: 'views/user/user.html',
    controller: 'UserCtrl'
})
  

http://localhost:9000/#/user/123456789