Angularjs ui-处于 html5 启用模式的路由器无法正常工作
Angularjs ui-router in html5 enabled mode not working
Angularjs版本1.3.14,ui-路由器版本0.2.15,html5mode启用。当我尝试 http://localhost/firsttime it redirects to http://localhost 作为后备
这里是app.js
'use strict';
// Declare app level module which depends on views, and components
angular.module('tt', [
'ngRoute',
'tt.home',
'tt.firsttime',
'ui.router'
])
.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
// Routes
$routeProvider.otherwise({
redirectTo: '/'
});
// set HTML5 history API
$locationProvider.html5Mode(true);
}]);
这里是firsttime.js
// create our angular app and inject ngAnimate and ui-router
// =============================================================================
angular.module('tt.firsttime', ['ui.router'])
// configuring our routes
// =============================================================================
.config(['$stateProvider', '$urlRouterProvider', '$routeProvider', function($stateProvider, $urlRouterProvider, $routeProvider) {
// catch all route
// send users to the form page
$urlRouterProvider.otherwise('/firsttime');
$stateProvider
// route to show our basic form (/firsttime)
.state('firsttime', {
url: '/firsttime',
templateUrl: 'firsttime/form.html',
//controller: 'formController'
})
}])
// our controller for the form
// =============================================================================
.controller('formController', ['$scope', function($scope) {
console.log('FORM controller');
}]);
这里是form.html
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<div id="form-container">
<div class="page-header text-center">
<h2>Let's Be Friends</h2>
<!-- the links to our nested states using relative paths -->
<!-- add the active class if the state matches our ui-sref -->
<div id="status-buttons" class="text-center">
<a ui-sref-active="active" ui-sref="firsttime.profile"><span>1</span> Profile</a>
<a ui-sref-active="active" ui-sref="firsttime.interests"><span>2</span> Interests</a>
<a ui-sref-active="active" ui-sref="firsttime.payment"><span>3</span> Payment</a>
</div>
</div>
<!-- use ng-submit to catch the form submission and use our Angular function -->
<form id="signup-form" ng-submit="processForm()">
<!-- our nested state views will be injected here -->
<div id="form-views" ui-view></div>
</form>
</div>
</div>
</div>
更新这里是nginx路由块
location / {
root html/app;
index index.html;
try_files $uri /index.html;
}
从 app.js
中删除可能与您当前的路线系统冲突的 config
块。你永远不应该同时使用这两个路由引擎。通过查看您的代码,您似乎正在尝试使用 ui-router
进行路由。因此,通过删除 app.js
配置块可能会解决您的问题。
问题是因为这条线
$routeProvider.otherwise({
redirectTo: '/'
});
从 app.js
中删除代码后,您应该将下面的行移动到 firsttime.js
配置块,这将启用 html5mode
// set HTML5 history API
$locationProvider.html5Mode(true);
Angularjs版本1.3.14,ui-路由器版本0.2.15,html5mode启用。当我尝试 http://localhost/firsttime it redirects to http://localhost 作为后备
这里是app.js
'use strict';
// Declare app level module which depends on views, and components
angular.module('tt', [
'ngRoute',
'tt.home',
'tt.firsttime',
'ui.router'
])
.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
// Routes
$routeProvider.otherwise({
redirectTo: '/'
});
// set HTML5 history API
$locationProvider.html5Mode(true);
}]);
这里是firsttime.js
// create our angular app and inject ngAnimate and ui-router
// =============================================================================
angular.module('tt.firsttime', ['ui.router'])
// configuring our routes
// =============================================================================
.config(['$stateProvider', '$urlRouterProvider', '$routeProvider', function($stateProvider, $urlRouterProvider, $routeProvider) {
// catch all route
// send users to the form page
$urlRouterProvider.otherwise('/firsttime');
$stateProvider
// route to show our basic form (/firsttime)
.state('firsttime', {
url: '/firsttime',
templateUrl: 'firsttime/form.html',
//controller: 'formController'
})
}])
// our controller for the form
// =============================================================================
.controller('formController', ['$scope', function($scope) {
console.log('FORM controller');
}]);
这里是form.html
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<div id="form-container">
<div class="page-header text-center">
<h2>Let's Be Friends</h2>
<!-- the links to our nested states using relative paths -->
<!-- add the active class if the state matches our ui-sref -->
<div id="status-buttons" class="text-center">
<a ui-sref-active="active" ui-sref="firsttime.profile"><span>1</span> Profile</a>
<a ui-sref-active="active" ui-sref="firsttime.interests"><span>2</span> Interests</a>
<a ui-sref-active="active" ui-sref="firsttime.payment"><span>3</span> Payment</a>
</div>
</div>
<!-- use ng-submit to catch the form submission and use our Angular function -->
<form id="signup-form" ng-submit="processForm()">
<!-- our nested state views will be injected here -->
<div id="form-views" ui-view></div>
</form>
</div>
</div>
</div>
更新这里是nginx路由块
location / {
root html/app;
index index.html;
try_files $uri /index.html;
}
从 app.js
中删除可能与您当前的路线系统冲突的 config
块。你永远不应该同时使用这两个路由引擎。通过查看您的代码,您似乎正在尝试使用 ui-router
进行路由。因此,通过删除 app.js
配置块可能会解决您的问题。
问题是因为这条线
$routeProvider.otherwise({
redirectTo: '/'
});
从 app.js
中删除代码后,您应该将下面的行移动到 firsttime.js
配置块,这将启用 html5mode
// set HTML5 history API
$locationProvider.html5Mode(true);