angularjs – Angular UI路由器 – 父級中的訪問狀態引數
我的設定看起來像:
.config( function ($stateProvider, $urlRouterProvider) { $stateProvider // PROJECT DETAIL .state('project', { url: '/project/:projectId/', resolve: { /* some base api calls */ }, views: { 'task-list': { templateUrl: 'partials/task_list.tmpl.html', controller: 'TaskListCtrl', resolve: { tasks: function ($stateParams, ConcernService) { return ConcernService.get('project-tasks/', $stateParams.projectId); }, } }, 'concern-instance': { /* some resolved variables */ } } }) .state('project.task', { url: 'task/:taskId/', views: { 'concern-instance@': { /* some different resolved variables */ }, } })
這一切都像黃油一樣工作.但是在我的task_list模板中,在狀態project.task中,我想要能夠訪問taskId引數,所以我可以突出顯示活動的導航連結,即使url是#/ project / 123 / task / 456 / taskId是空的.我明白這是因為模板只能訪問為該狀態宣告的引數.所以如果我想在project.task狀態下獲取taskId,該怎麼辦?我需要在porject.task中重新宣告任務列表嗎?
任何幫助非常感謝.
中,他們解釋說
“the $stateParams object will only contain the params that wereregistered with that state.”
您只能使用父級的resolve屬性訪問子狀態中的父狀態引數.
把它放在你的’專案’狀態(父):
... resolve: { // Expose projectId parameter to child states projectId: ['$stateParams', function ($stateParams) { return $stateParams.projectId; }] },
然後在您的控制器內為您的狀態“project.task”(子),您應該可以訪問這兩個
$stateParams.projectId
和
$stateParams.taskId
http://stackoverflow.com/questions/22946983/angular-ui-router-access-state-params-in-parent