Can I disable $ locationProvider and $ routeProvider?

Can you disconnect these guys? I use angular.js in an asp.net mvc application and I don't need angular to control everything related to the address bar or links ...

Currently disabled in html5 mode ( $locationProvider.html5Mode(false) ), it adds the hash name and action method name to the address bar, for example: you go to \Home\index , it moves, and then the text in the address bar in Home\index#\index changes Home\index#\index . Isn't that annoying?

if I enable html5 mode, it generally stops loading pages (except the initial one). I am trying to move from the initial loaded page to another - it changes the text of the address bar (without adding a hashtag this time), but does not load the page itself. Isn't that upsetting?

+4
source share
1 answer

There is a workaround for AngularJS 1.1.5 - automatically adding a hash tag to URLs

The answer explains the first step (as explained above, with the addition of a new hash prefix)

 yourApp.config(['$locationProvider', function($locationProvider){ $locationProvider.html5Mode(true).hashPrefix('!'); }]); 

The first bit handles Angular visually visually, but clicking on any links does not work properly (read: history.pushState )

So, a workaround, as @Kevin Beal pointed out, is some variation of the target setting from <a> to _self

 $('a[href]').attr({'target':'_self'}) 

or in each case:

 <a href="foo" target="_self">Foo</a> <a href="http://some.external/bar" target="_blank">Bar</a> 

Although, for convenience and sanity, I think this is a combination of them.

Markup without target

 <a href="foo">Foo</a> <a href="http://some.external/bar">Bar</a> 

Js

 // If `http` protocol present, assume external link $('a[href^="http://"]').attr({'target':'_blank'}); // Otherwise, assume internal link $('a:not([href^="http://"])').attr({'target':'_self'}); 

It is worth noting that the above selectors really require jQuery itself.

+5
source

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


All Articles