gpt4 book ai didi

javascript - AngularJS:使用history.pushState()删除 "#"

转载 作者:行者123 更新时间:2023-11-28 01:05:02 29 4
gpt4 key购买 nike

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

Note: The project is located at (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)

console.log dump

最佳答案

已解决:<base>是罪魁祸首

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

或者

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

关于javascript - AngularJS:使用history.pushState()删除 "#",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25185393/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com