我使用 Angular Routes 来确定要向用户显示的 View ,如下所示:
define(['angular', './app'], function(angular, app) {
'use strict';
return angular.module('app', ['ngRoute']).
config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl:'assets/partials/partial1.html',
controller: 'MyCtrl1'
}).when('/mada', {
templateUrl:'assets/partials/partial2.html',
controller: 'MyCtrl2'
})
.otherwise({redirectTo: '/'});
});
});
目前,路由可以工作,但只能这样:localhost/#/mada 或 localhost/#/ 我希望能够看到 localhost/mada
我知道添加
$locationProvider.html5Mode(true);
将摆脱散列。但这仍然无法让用户访问:localhost/mada,因为这会返回服务器 404。
这能实现吗?怎么办?
最佳答案
如果您不使用服务器端语言,则将此代码放在 html head 中。
<base href="/">
喜欢:
<head>
<base href="/">
</head>
并将其放入配置 block 中:
$locationProvider.html5Mode(true);
如果您使用服务器端语言,请使用以下代码:
app.get('*', function(req, res) {
res.render('index');
});
而不是
app.get('/', function(req, res) {
res.render('index');
});
并将此代码放入配置 block 中:
$locationProvider.html5Mode(true);
关于javascript - AngularJS 与 RequireJS 路由删除哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25952457/