Removing # from url in Angularjs while having .run in routes

后端 未结 7 663
感情败类
感情败类 2020-11-27 07:02

Here is my app.js route file in AngularJS

var app = angular.module(\'myApp\', [\'ngRoute\', \'ngAnimate\', \'toaster\']);
app.config([\'$routeP         


        
7条回答
  •  长情又很酷
    2020-11-27 07:38

    You can't inject $locationProvider in .run because providers are only available for .config

    This answer should help but you will have 2 issues:

    1. You will need a check before $locationProvider.html5Mode(true) as it will not work on IE 10 or older.

      if(window.history && window.history.pushState){
          $locationProvider.html5Mode(true);
      } 
      
    2. This will work only by removing the # when the user enters it in the url, i.e. if the users types app/#/login it will change to app/login. However, if the user bookmarks or copies the changed url app/login and enters that in the browser he will get an error as the server does not know about angular routing since it is client side only. In the thread I linked above you may find some comments on how to fix this.

提交回复
热议问题