AngularJS:使用history.pushState()删除“#”

时间:2014-08-07 14:41:10

标签: javascript ajax angularjs .htaccess pushstate

我试图在我的Angularjs应用程序中美化网址,但我的成功有限 - 我可以达到"/"路线,但那就是它,{{ 1}}无法访问。

  

注意:项目位于("/about")。

的index.html

localhost/myngapp

app.js

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <base href="myngapp">
    <script  type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.js"></script>
    <script  type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular-route.js"></script> 
    <script  type="text/javascript" src="http://localhost/myngapp/public/js/app.js"></script>   
</head>

<body data-ng-app="demoApp">
    <div data-ng-view></div>
</body>

</html>

的.htaccess

angular.module('demoApp', ['ngRoute'])
    .config(function ($routeProvider, $locationProvider) { 
        $locationProvider.html5Mode(true); 
        $routeProvider
             .when('/', {
                 controller: 'homeCtrl',
                templateUrl: 'home.html'
             })         
             .when('/about', {
                 controller: 'aboutCtrl',
                 templateUrl: 'about.html'
             })     
            .otherwise({ redirectTo: '/' });       
    })
    .controller('homeCtrl', function ($scope) {
         $scope.title = 'This is, the App!';
    })
    .controller('aboutCtrl', function ($scope) {
         $scope.title = 'About App!';
    });

console.log(<ifModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^ index.html [L] </ifModule>

console.log dump

1 个答案:

答案 0 :(得分:1)

解决:<base>是罪魁祸首

<head>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
</head>

OR

<head>
  <base href="/myngapp/">
</head>