我想爲我的意思應用使用html5模式。我的角色腳本中的視圖路由器代碼如下。在html5模式下使用ng-route重新加載角度頁面不會返回index.html
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider
// enumerate routes
// {tempUrl: req to send to express,
// cntrl: ng-cntrl to be used}
.when('/', {
templateUrl: '/home',
controller: 'mainController'
})
.when('/contact', {
templateUrl: '/contact',
controller: 'mainController'
})
.when('/team', {
templateUrl: '/team',
controller: 'mainController'
});
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
}]);
我已經把基本的標籤在我的index.html文件的開頭如下:
...
<base href="/" />
</head>
<body ng-app="mainPage">
<header>
<nav class="navbar navbar-static-top" role="navigation">
<div>
<div class="navbar navbar-top">
<ul class="nav navbar-nav float-right">
<li ng-class="{ active: isActive('/')}"><a class="link-btn" href="">Home</a></li>
<li ng-class="{ active: isActive('/team')}"><a class="link-btn" href="team">Team</a></li>
<li ng-class="{ active: isActive('/lessons')}"><a class="link-btn" href="lessons">Lessons</a></li>
<li ng-class="{ active: isActive('/media')}"><a
</ul>
</div>
</div>
</nav>
<div ng-controller="mainController">
<div id="main">
<div class="container-fluid">
<div ng-view></div>
</div>
</div>
</div>
...
我也加入了此規則,以我的web.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<!-- indicates that the server.js file is a Node.js application
to be handled by the iisnode module -->
<handlers>
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<clear />
<rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="iisnode.+" negate="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="app.js" />
</rule>
<rule name="Main Rule" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
在我app.js文件:
app.get('/', routes.index);
app.get('/home', routes.home);
app.get('/contact', routes.contact);
app.get('/team', routes.team);
app.get('/lessons', routes.lessons);
app.get('/media', routes.media);
app.get('/signup', routes.signUp);
app.post('/new', users.new);
//app.get('*', routes.index); uncommenting the last line causes
//index.html to render without css, even though my stylseete is being served.
//this is confusing to me because I do want to always serve the index page
問題是當我導航到像/團隊或/聯繫頁面然後刷新頁面時,只有部分服務沒有樣式。我想提供索引頁面,然後將部分渲染到ng-route元素中。我知道這需要重寫我的節點服務器代碼,並嘗試了幾次重寫而沒有成功。我看過這Reloading the page gives wrong GET request with AngularJS HTML5 mode。我看到有很多關於使用html5模式的問題,但我已經嘗試了很多解決方案而沒有獲得所需的行爲。任何幫助是極大的讚賞。 感謝