2016-06-23 85 views
0

我有以下的HTML頁面:路由使用AngularJS

<!DOCTYPE html> 
<html> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script> 

<body ng-app="myApp"> 

<p><a href="/">Main</a></p> 

<a href="first">City 1</a> 
<a href="#first">City 1a</a> 

<div ng-view></div> 

<script> 
var app = angular.module("myApp", ["ngRoute"]); 
app.config(function($routeProvider) { 
    $routeProvider 
    .when("/first", { 
     templateUrl : "first.html" 
    }) 
    .when("/first", { 
     templateUrl : "first.html" 
    }); 
}); 
</script> 

</body> 
</html> 

然後有一個簡單的頁面first.html。 現在,我的City1a鏈接工程和angularJS正確加載頁面到<div ng-view></div>。相反,city1鏈接不起作用,我有404錯誤。我的網址是http://localhost/myproject/index.html#/first。我能做些什麼來打開http://localhost/myproject/first的URL?

回答

0
module.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { 
     $locationProvider.html5Mode(true); 
     //...your routes 
} ]} 

這就迫使角度使用HTML5歷史API,它允許你操縱瀏覽器歷史和不使用哈希值與位置操縱。

+0

它不起作用。我沒有404錯誤,但沒有加載first.html頁面。 – DistribuzioneGaussiana

+0

和哪個錯誤被拋出? – VahagnNikoghosian