2016-03-03 87 views
0

我想在不刷新頁面的情況下更改網址。例如我的網址是http://test.com?search=test更改查詢字符串,無需重新加載/刷新

當我點擊像「類別」的搜索頁面上的任何過濾器值,那麼我的網址應該是:

http://test.com?search=test$category=cat1 

當我點擊另一類別應該是:

http://test.com?search=test&category=cat1,cat2 

當我點擊一個品牌那麼URL應該是:

http://test.com?search=test$category=cat1,cat2&brand=br1 

當我取消選擇一個類別,然後網址應該是:

http://test.com?search=test$category=cat2&brand=br1 

請幫我。

+0

你使用的是路由器嗎? –

+0

不,我不使用ui路由器 –

+1

我有同樣的問題。閱讀[this](http://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-page)。 – jmtb

回答

1

您可以通過歷史API

window.history.pushState("object or string", "Title", "/new-url"); 

在這種情況下,相對的/新網址將與新標題進行設置更改。您可以在控制檯中試用它。如果你輸入這個在stackoverflow.com網址會奇蹟般地改變爲https://stackoverflow.com/new-url

+1

這是不正確的。您可以:HTML5歷史API。 – undefined

+0

@Vohuman絕對正確! – Kordi

+0

我認爲在角度上,爲了激活HIstory API,應該使用'$ locationProvider'服務,它是'.html5Mode(true)'方法。 – undefined