角度UI路由无法在页面重新加载或直接URL命中

时间:2017-03-09 05:40:22

标签: html angularjs angular-ui-router virtualhost

当我点击ui-sref =“state1”或ui-sref =“state”它工作正常时,将网址更改为http://demo/state1http://demo/state2,但直接点击网址时不是工作enter image description here

HTML CODE

<html>
<base href="/">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.2/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.4.2/angular-ui-router.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="personCtrl">


    <div ui-view></div>
    <!-- We'll also add some navigation: -->
    <a ui-sref="state1">State 1</a>
    <a ui-sref="state2">State 2</a>

</div>
</body>
</html>

JS代码

var app = angular.module('myApp', ['ui.router']);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
    $scope.fullName = function() {
        return $scope.firstName + " " + $scope.lastName;
    };
});
app.config(function($stateProvider,$locationProvider, $urlRouterProvider) {
  // 
  // For any unmatched url, redirect to /state1 
  $urlRouterProvider.otherwise("/state1");
  // 
  // Now set up the states 
  $stateProvider
    .state('state1', {
      url: "/state1",
      template: "<div>state1</div>"
    })

    .state('state2', {
      url: "/state2",
      template: "<div>state2</div>"
    })
    $locationProvider.html5Mode(true);
});

1 个答案:

答案 0 :(得分:1)

您需要为服务器编写网址重写规则,以便针对任何请求重定向到index.html。

Look here for details