启用html5的Angularjs ui-router模式无法正常工作

时间:2015-07-25 07:30:55

标签: javascript angularjs html5 angular-ui-router angularjs-routing

Angularjs版本 1.3.14 ,ui-router版本 0.2.15 ,html5mode 已启用。当我尝试 http://localhost/firsttime 时,它会重定向到 http://localhost 作为后备

这是 app.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');
}]);

这是 firsttime.js

<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>

这是 form.html

    location / {
        root   html/app;
        index  index.html;
        try_files $uri /index.html;
    }

更新这是nginx路由块

getSupportFragmentManager()

1 个答案:

答案 0 :(得分:1)

config中删除可能与您当前路由系统冲突的app.js块。你永远不应该同时使用两个路由引擎。通过查看代码,您似乎正在尝试使用ui-router进行路由。因此,删除app.js配置块可能会解决您的问题。

问题在于这一行

$routeProvider.otherwise({
    redirectTo: '/'
});

app.js删除代码后,您应该移到firsttime.js配置块下面的行,这将启用html5mode

// set HTML5 history API
$locationProvider.html5Mode(true);