2016-02-05 122 views
0

我希望能够重新加载我的应用程序的嵌套视图并附加一个路由参数,以便我的应用程序中可以有URL路由。我无法弄清楚如何做到这一点,我最初有它的查询工作是这样的:AngularJS中的URL路由参数ui-router

$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' 
      }, 
      '[email protected]' : { 
       templateUrl: 'views/top.html', 
       controller: function($scope, $state) { 
        $scope.userLogOut = function() { 
         $state.go('login'); 
        }; 
       } 
      }, 
      '[email protected]' : { templateUrl: 'views/left.html' }, 
      '[email protected]' : { templateUrl: 'views/main.html' } 
     } 
    }) 
    .state('index.user', { 
     url: 'user:userId', 
     templateUrl: 'views/user/user.html', 
     controller: 'UserCtrl' 
    }) 
    .state('index.user.detail', { 
     url: '/', 
     views: { 
      '[email protected]' : { 
       templateUrl: 'views/user/details.html', 
       controller: 'DetailCtrl' 
      } 
     } 
    }) 

在我的控制器:

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

回答

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

Witho UT斯达康的查询字符串参数(你想要的网址)会是这样,

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

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

+0

'$ state.go( 'index.user',{用户名:{{yourid}}} );'不会加载'.detail'的嵌套状态。然后,如果我尝试'$ state.go('index.user.detail',{userId:{{yourid}}});'我不重新加载视图,因为角度认为我已经在我想要的视图。我无法添加'{reload:true}',因为它只是在循环中重新加载。我希望能够设置URL并重新载入'.details'视图 – tester123