我正在尝试美化Angularjs应用程序中的URL,但是我取得的成功有限-我可以到达"/"路线,仅此而已,"/about"无法访问。


  注意:该项目位于(localhost/myngapp)。


index.html

<!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>


app.js

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!';
    });


.htaccess

<ifModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^ index.html [L]

</ifModule>


console.log(http://localhost/myngapp/ || http://localhost/myngapp/about

最佳答案

解决:<base>是罪魁祸首

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


要么

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

07-26 09:39