Reloading the page gives wrong GET request with AngularJS HTML5 mode

前端 未结 24 2957
慢半拍i
慢半拍i 2020-11-22 01:39

I want to enable HTML5 mode for my app. I have put the following code for the configuration, as shown here:

return app.config([\'$routeProvider\',\'$location         


        
24条回答
  •  悲&欢浪女
    2020-11-22 02:13

    There are few things to set up so your link in the browser will look like http://yourdomain.com/path and these are your angular config + server side

    1) AngularJS

    $routeProvider
      .when('/path', {
        templateUrl: 'path.html',
      });
    $locationProvider
      .html5Mode(true);
    

    2) server side, just put .htaccess inside your root folder and paste this

    RewriteEngine On 
    Options FollowSymLinks
    
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /#/$1 [L]
    

    More interesting stuff to read about html5 mode in angularjs and the configuration required per different environment https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode Also this question might help you $location / switching between html5 and hashbang mode / link rewriting

提交回复
热议问题