2016-02-05 117 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