Angular support for both hash and normal url?

I am very new to Angularjs, so please excuse me if this is an obvious question. We have a single-page Ajgularjs application that works with data after a hash. For example.

http://server/myapp#/user/777?accountid=123&arranger=true

Pay attention to the hash.

This usually works. Unfortunately, we are faced with some new scenario where we cannot use a hash (a deep link sometimes passes through an authentication environment out of our control and it removes the hash part of the URL). Therefore, we also need links without a hash, for example.

http://server/myapp/user/777?accountid=123&arranger=true

Note. I am not against the exact format, just note that it does not contain a hash.

Question: Is there a reasonable way to support both approaches so that the application responds similarly to the two types of links? (we need a hash format for backward compatibility with old links, and we need a hash less format for a new authentication scenario)

Many thanks

+4
source share
1 answer

In a service $location, angular makes all parts of the URL accessible to the application.

// given url http://example.com/#/some/path?foo=bar&baz=xoxo
var url = $location.url();
// => "/some/path?foo=bar&baz=xoxo"

// given url http://example.com/#/some/path?foo=bar&baz=xoxo
var path = $location.path();
// => "/some/path"

// given url http://example.com/#/some/path?foo=bar&baz=xoxo#hashValue
var hash = $location.hash();
// => "hashValue"

I'm not sure what your specific situation is, but you can use an event handler $routeChangeStart(or $stateChangeStartwhen using ui-router) to intercept the route and use the appropriate method to $locationfind out which route you would like to go to. Then set the URL to go to the decoded route in the application.

0

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


All Articles