Angularjs的ui-router在html5启用模式下无法工作

Angularjs ui-router in html5 enabled mode not working

本文关键字:工作 模式 启用 ui-router html5 Angularjs      更新时间:2023-09-26

Angularjs版本1.3.14, ui-router版本0.2.15, html5mode enabled。当我尝试http://localhost/firsttime它重定向到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>

Update这是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);