2016-08-02 84 views
0

我正在開發一個使用angular-ui-router的angularjs應用程序。有一個列出項目的頁面,可以通過名稱進行篩選,並且它顯示在URL中,如/items?name = foo。還有一個頁面,例如用戶,從那裏你可以導航到項目頁面,並按用戶過濾,如/items?userid = barAngularJS一個多頁狀態頁面

我這樣做,通過在路由器配置在同一頁的兩種狀態:

.state('index.items', { 
    url: '/items?:name', 
    controller: 'ItemCtrl as ctrl', 
    templateUrl: 'item/index.html', 
    brParent: 'index.home' 
}) 
.state('index.itemsOfUser', { 
    url: '/items?:userid&:name', 
    controller: 'ItemCtrl as ctrl', 
    templateUrl: 'item/index.html', 
    brParent: 'index.home' 
}) 

我這樣做,所以點擊導航條上的項目頁轉到正常的項目頁面而不是用戶過濾。

但是,當從用戶導航到項目頁面時,userid出現在url中,但在$ stateparams userid中是未定義的,我不明白爲什麼。

我不知道我的方法是否正確,我是新的角度。

+0

你看看'state.reloadOnSearch'? ...順便說一下,不是一個好方法在不同的應用程序實體之間共享相同的狀態,你不應該聲明許多狀態具有相同的url ... – Hitmands

+0

嗯,我也(試圖)問是否是一種好方法爲相同頁面或相同網址聲明多個州,或者我應該搜索/找出另一個解決方案。謝謝! – nolley

回答

2

ui-router使得能夠檢索查詢字符串參數。你的代碼幾乎是正確的,只是一個小語法修復:

.state('index.items', { 
    url: '/items?name', // Remove semicolon 
    controller: 'ItemCtrl as ctrl', 
    templateUrl: 'item/index.html', 
    brParent: 'index.home' 
}) 
.state('index.itemsOfUser', { 
    url: '/items?userid&name', // Remove semicolons 
    controller: 'ItemCtrl as ctrl', 
    templateUrl: 'item/index.html', 
    brParent: 'index.home' 
}) 

如文檔指出:https://github.com/angular-ui/ui-router/wiki/URL-Routing#query-parameters

+0

好吧,它和以前一樣,用冒號。也許問題不在於路由?如果一切正常,這不應該有任何問題? – nolley

+0

你可以上傳控制器代碼嗎?讓我們看看'$ stateparams'的用法 – AranS

+0

它就像[this](http://pastebin.com/gUnUcG21)。我發現當頁面用userid加載時,它在$ stateparams中,但在dofilter重新加載之後,它丟失了。 – nolley