$location / switching between html5 and hashbang mode / link rewriting

前端 未结 4 1658
情深已故
情深已故 2020-11-22 01:49

I was under the impression that Angular would rewrite URLs that appear in href attributes of anchor tags within tempaltes, such that they would work whether in html5 mode or

4条回答
  •  春和景丽
    2020-11-22 02:37

    I wanted to be able to access my application with the HTML5 mode and a fixed token and then switch to the hashbang method (to keep the token so the user can refresh his page).

    URL for accessing my app:

    http://myapp.com/amazing_url?token=super_token

    Then when the user loads the page:

    http://myapp.com/amazing_url?token=super_token#/amazing_url

    Then when the user navigates:

    http://myapp.com/amazing_url?token=super_token#/another_url

    With this I keep the token in the URL and keep the state when the user is browsing. I lost a bit of visibility of the URL, but there is no perfect way of doing it.

    So don't enable the HTML5 mode and then add this controller:

    .config ($stateProvider)->
        $stateProvider.state('home-loading', {
             url: '/',
             controller: 'homeController'
        })
    .controller 'homeController', ($state, $location)->
        if window.location.pathname != '/'
            $location.url(window.location.pathname+window.location.search).replace()
        else
            $state.go('home', {}, { location: 'replace' })
    

提交回复
热议问题