2015-12-09 28 views
0

如果我不包含$ urlRouterProvider.otherwise('/');在配置中。ui路由無urlRouterProvider.otherwise

與大猩猩我去App Engine的設置是

r := mux.NewRouter() 
r.HandleFunc(/admin, handleAdmin) 

和angularjs配置是;

angular.module('admin') 
    .config(function($stateProvider, $urlRouterProvider) { 
     $stateProvider.state('home', { 
     url: '/', 
     templateUrl: '/admin/index.html', 
     controller: 'AdminController as admin' 
     }); 
    //$urlRouterProvider.otherwise('/'); 
}) 

的時候我不叫$ urlRouterProvider.othwerwise行,我打開http://localhost:8080/admin我希望看到管理員/ index.html的,但我不知道。我只有在手動導航到http://localhost:8080/admin#/時才能看到它。 但是,如果我添加$ urlRouterProvider.othwerwise選項並轉到http://localhost:8080/admin它會自動重定向到http://localhost:8080/admin#/ 我不認爲這是通常的方式來做到這一點,因爲我可能需要「否則」路由到自定義404頁面。我錯過了什麼?

回答

0

默認情況下,Angular在url前添加hashPrefix。因此,當您導航到http://localhost:8080/admin時,由於您尚未訪問angular的ui路由器中定義的url,您看不到index.html。您必須導航到http://localhost:8080/admin/#/才能確定您的應用程序的/狀態。

這與您的應用在沒有.otherwise()的情況下無法正常工作的原因相同,因爲此後它會在稍後自動將您重定向到/狀態。

對於一個可能的解決辦法:

裏面你.config功能:

// This is in your app module config. 
$locationProvider.html5Mode(true); 

而在你index.html

// This is in your index.html head. 
<base href="/" /> 
+0

好吧我嘗試了這一點,它的工作原理(儘管在控制檯窗口中有很多錯誤),但我想要相同的功能,當我去http:// localhost在另一個模塊負責,我使用ngNewRouter,它會自動重定向到http: // localhost:8080 /#/而html5未啓用。 – nurp

+0

http:// localhost:8080/admin#/ works但是http:// localhost:8080/admin /#/沒有。 – nurp

+1

您的應用程序必須有一個主模塊,其中注入所有其他模塊,並將其附加到該模塊的配置功能,以使其在整個應用程序中運行。 –