The nested [[UrlConfig]] API to configure the URL and retrieve URL information
See: [[UrlConfig]] for details
Gets the hash part of the current url
If the current URL is /some/path?query=value#anchor
, this returns anchor
Creates a new [[Param]] for a given location (DefType)
Gets the path part of the current url
If the current URL is /some/path?query=value#anchor
, this returns /some/path
the path portion of the url
The nested [[UrlRules]] API for managing URL rules and rewrites
See: [[UrlRules]] for details
Gets the search part of the current url as an object
If the current URL is /some/path?query=value#anchor
, this returns { query: 'value' }
the search (query) portion of the url, as an object
Provides services related to the URL
Static
$injectCreates a [[UrlMatcher]] for the specified pattern.
The URL pattern.
The config object hash.
The UrlMatcher.
Disables monitoring of the URL.
Call this method before ng-router has bootstrapped. It will stop ng-router from performing the initial url sync.
This can be useful to perform some asynchronous initialization before the router starts. Once the initialization is complete, call [[listen]] to tell ng-router to start watching and synchronizing the URL.
// Prevent ng-router from automatically intercepting URL changes when it starts;
urlService.deferIntercept();
fetch('/states.json').then(resp => resp.json()).then(data => {
data.forEach(state => $stateRegistry.register(state));
urlService.listen();
urlService.sync();
});
Indicates whether to defer location change interception.
Passing no parameter is equivalent to true
.
Builds and returns a URL with interpolated parameters
matcher = $umf.compile("/about/:person");
params = { person: "bob" };
$bob = $urlService.href(matcher, params);
// $bob == "/about/bob";
The [[UrlMatcher]] object which is used as the template of the URL to generate.
An object of parameter values to fill the matcher's required parameters.
Options object. The options are:
absolute
- {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl".Returns the fully compiled URL, or null
if params
fail validation against urlMatcher
Starts or stops listening for URL changes
Call this sometime after calling [[deferIntercept]] to start monitoring the url. This causes ng-router to start listening for changes to the URL, if it wasn't already listening.
If called with false
, ng-router will stop listening (call listen(true) to start listening again).
urlService.deferIntercept();
fetch('/states.json').then(resp => resp.json()).then(data => {
data.forEach(state => $stateRegistry.register(state));
// Start responding to URL changes
urlService.listen();
urlService.sync();
});
true
or false
to start or stop listening to URL changes
Internal
Registers a low level url change handler
Note: Because this is a low level handler, it's not recommended for general use.
let deregisterFn = locationServices.onChange((evt) => console.log("url change", evt));
a function that will be called when the url is changing
a function that de-registers the callback
Gets the current URL parts
This method returns the different parts of the current URL (the [[path]], [[search]], and [[hash]]) as a [[UrlParts]] object.
Activates the best rule for the current URL
Checks the current URL for a matching [[UrlRule]], then invokes that rule's handler. This method is called internally any time the URL has changed.
This effectively activates the state (or redirect, etc) which matches the current URL.
urlService.deferIntercept();
fetch('/states.json').then(resp => resp.json()).then(data => {
data.forEach(state => $stateRegistry.register(state));
urlService.listen();
// Find the matching URL and invoke the handler.
urlService.sync();
});
Gets the current url, or updates the url
When no arguments are passed, returns the current URL. The URL is normalized using the internal [[path]]/[[search]]/[[hash]] values.
For example, the URL may be stored in the hash ([[HashLocationServices]]) or have a base HREF prepended ([[PushStateLocationServices]]).
The raw URL in the browser might be:
http://mysite.com/somepath/index.html#/internal/path/123?param1=foo#anchor
or
http://mysite.com/basepath/internal/path/123?param1=foo#anchor
then this method returns:
/internal/path/123?param1=foo#anchor
locationServices.url(); // "/some/path?query=value#anchor"
When newurl
arguments is provided, changes the URL to reflect newurl
locationServices.url("/some/path?query=value#anchor", true);
Optional
newUrl: stringThe new value for the URL. This url should reflect only the new internal [[path]], [[search]], and [[hash]] values. It should not include the protocol, site, port, or base path of an absolute HREF.
Optional
replace: boolean = falseWhen true, replaces the current history entry (instead of appending it) with this new url
Optional
state: anyThe history's state object, i.e., pushState (if the LocationServices implementation supports it)
the url (after potentially being processed)
API for URL management