2016-05-14 103 views
0

documentation訪問參數據說:從UI路由器的孩子的狀態

解析的依賴通過決心

自定義數據:

兒童國家確實從父狀態繼承以下屬性

沒有其他東西被繼承(沒有控制器,模板,網址等)。 但是,抽象狀態的子女會繼承父級的url屬性 作爲其自己的url的前綴。

這是否意味着在我的孩子狀態,我將有能力解析整個請求字符串?

例如如果我有狀態的層次結構如下:

$stateProvider 
    .state('contacts', { 
    url: 'contacts/:contactId', 
    abstract: true, 
    /* ... */ 
    }) 
    .state('contacts.details', { 
    url: '/details', 
    controller: 'ContactDetailsController', 
    /* ... */ 
    }); 

會注入$stateParamsContactDetailsController訪問contactId一個,就是在contacts抽象的狀態下拍攝的?

+0

你試過了嗎?如果是這樣的工作示例? – charlietfl

+1

是的,如果你的url以'contacts/1/details'結尾,你可以在'ContactDetailsController'中使用$ stateParams獲得'contactId' – Saad

+0

apprently not :(參見https://github.com/angular-ui/ui-路由器/維基/ URL路由#重要-stateparams,疑難雜症 – zmii

回答

0

documentation找到答案:

重要$ stateParams疑難雜症

在狀態控制器,該$stateParams對象將只包含已與該國註冊的PARAMS。所以你不會看到其他國家,包括祖先註冊的參數。

$stateProvider.state('contacts.detail', { 
    url: '/contacts/:contactId', 
    controller: function($stateParams){ 
     $stateParams.contactId //*** Exists! ***// 
    } 
}).state('contacts.detail.subitem', { 
    url: '/item/:itemId', 
    controller: function($stateParams){ 
     $stateParams.contactId //*** Watch Out! DOESN'T EXIST!! ***// 
     $stateParams.itemId //*** Exists! ***// 
    } 
}) 

相反,在父路由中使用resolve語句。

$stateProvider.state('contacts.detail', { 
    url: '/contacts/:contactId', 
    controller: function($stateParams){ 
     $stateParams.contactId //*** Exists! ***// 
    }, 
    resolve:{ 
     contactId: ['$stateParams', function($stateParams){ 
      return $stateParams.contactId; 
     }] 
    } 
}).state('contacts.detail.subitem', { 
    url: '/item/:itemId', 
    controller: function($stateParams, contactId){ 
     contactId //*** Exists! ***// 
     $stateParams.itemId //*** Exists! ***// 
    } 
})