[英]Strange angularjs route behaviour
我的angularjs應用上有以下路線:
angular.module('index').config(['$routeProvider', '$locationProvider',
function indexRouteConfig($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
var routeToUserHomePage = ['$injector', function routeToUserHomePage($injector) {
var $location = $injector.get('$location');
//somecode
$location.url('/unauthorized'); // this works
}
$routeProvider
.when('/', {
templateUrl : 'app/custom/templates/customTemplate.html',
controller : 'customTemplateController',
resolve : { routeToUserHomePage: routeToUserHomePage }
})
.when('/unauthorized', {
templateUrl : 'app/custom/templates/customTemplate.html',
controller : 'customTemplateController'
})
.otherwise({
resolve : { routeToUserHomePage: routeToUserHomePage }
});
如果我訪問URL http://localhost:8090/
則它可以正常運行,並且Im重定向到了unauthorized
屏幕。
但是,如果我轉到瀏覽器並輸入http://localhost:8090/unauthorized
則會從網絡服務器收到未找到錯誤404的信息。 它甚至沒有擊中“否則”路線。 我在這里想念什么?
我在index.html文件上有<base href="/">
標記。
創建一個單獨的控制器和模板,然后在該控制器中編寫邏輯
.otherwise ({
url: '/unauthorized',
controller: 'error',
templateUrl: 'templateUrl.html',
});
function error($location) {
// write whatever your logic
}
這是因為您已啟用html5Mode
,而服務器上沒有它所需的內容。
服務器端使用此模式需要在服務器端重寫URL,基本上,您必須重寫指向應用程序入口點的所有鏈接(例如index.html)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.