2015-12-09 29 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

您的应用程序必须有一个主模块,其中注入所有其他模块,并将其附加到该模块的配置功能,以使其在整个应用程序中运行。 –