Understanding what it takes to remove the hash # from angular routes

后端 未结 1 501
别跟我提以往
别跟我提以往 2020-11-28 16:14

Before removing the hash sign, I had

mainApp.config(function ($locationProvider, $routeProvider) {
    $routeProvider
    .when(\'/page\', {
        controll         


        
相关标签:
1条回答
  • 2020-11-28 16:45

    That's expected. Here's what happens when html5 is not turned on:

    • you enter the url http://localhost:8080/index.html#/main in the address bar
    • the browser makes a http request to localhost:8080/index.html and gets the html page as a response
    • the html page contains an angular application that is executed. The angular router parses the path after the hash (/main), and thus loads the associated view and controller

    Now what happens when you enable html5 mode and load index.hml?

    • you enter the url http://localhost:8080/index.html in the address bar
    • the browser makes a http request to localhost:8080/index.html and gets the html page as a response
    • the html page contains an angular application that is executed. The angular router parses the path (/index.html), sees that it doesn't match any route, and thus changes the location in the address bar to the default: /main, and then loads the associated view and controller. Changing the location in the address bar doesn't make the browser do anything other than adding a new entry in its navigation history.

    Now, what happens if you hit refresh, or directly enter http://localhost:8080/main in the address bar? Well in that case, you're saying the browser: "please load the page at the url http://localhost:8080/main. So that's what the browser does: it sends an HTTP request to http://localhost:8080/main. Since the server doesn't have anything at this address, it sends back a 404.

    Now how to make it work? It's actually quite simple: you need to configure the server to send back the index.html page when it gets a request for the path /main (and for all the other paths of the angular application). That way, the browser will load the HTML page, the angular application it contains will be restarted, the angular router will parse the path (/main) from the URL, and it will thus load the view and the controller associated to that path.

    0 讨论(0)
提交回复
热议问题