$routeProvider not working with html5Mode

给你一囗甜甜゛ 提交于 2021-01-28 02:21:40

问题


I just start to learn angularJs, and was trying to configure the partial page with angular route service.

It worked with the hash format, however,when I tried to get rid of hash, the routeProvider stopped working.

JS

app.config(function($routeProvider,$locationProvider){
    $locationProvider.html5Mode(true);
    $routeProvider.when("/", { templateUrl: "/index.html" }).
        when("/about", { templateUrl: "/partials/about.html" }).
        when("/contact", { templateUrl: "/partials/contact.html" }).
        otherwise({ redirectTo: '/' });
});

HTML

          <ul class="nav navbar-nav navbar-right">
              <li><a href="/about">About</a></li>
              <li><a href="/contact">Contact</a></li>
          </ul

Can someone enlighten me?


回答1:


Try to change

 $locationProvider.html5Mode(true);

to

$locationProvider.html5Mode(true).hashPrefix('!');

And add

<base href="/">

in a document "head" section.



来源:https://stackoverflow.com/questions/20696498/routeprovider-not-working-with-html5mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!