Angular.js add #% 2F by URL when setting $ locationProvider.html5Mode (true) .hashPrefix ('!')

After setting: $locationProvider.html5Mode(true).hashPrefix('!') When I enter the URL: www.example.com/search in the browser (chrome) and press enter, then it will turn into: www.example.com/#%2Fsearch , and then go to the home page.

when I click a link, for example <a href="/search" /> , then its work. if I remove .hashPrefix('!') , it also works fine. But I need hashPrefix('!')

Any idea how I can solve this problem?

+5
source share
3 answers

This happened to me because all my links had a β€œ#” on them, for example, at www.example.com/search/#/xxx/yyy . When you click, it will lead you to www.example.com/search/#%2Fxxx%2Fyyy . So I just deleted them and the problem disappeared.

+5
source

I know this is an old post and the problem has already been resolved. I had the same problem with the application I was working in, but in my case we have to save the '#'. So, all I did was add the following line:

 $locationProvider.html5Mode(false).hashPrefix(''); 

Remember to change your .config arguments if you have not already done so, for example:

 .config(function($routeProvider, $locationProvider) 
+4
source
 Angular.module('app', ['ngRoute']).config(function($routeProvider, $locationProvider) { $locationProvider.html5Mode(true); $locationProvider.hashPrefix('!'); $routeProvider.when("/", { templateUrl : "templates/home.html" }).when('/register',{ templateUrl : "http://localhost:3000/templates/register.html" }) // use the HTML5 History API });; 
-1
source

Source: https://habr.com/ru/post/1200051/


All Articles