Location
A service that applications can use to interact with a browser's URL.
path
stringNormalizes the URL path for this location.
booleanTrue to include an anchor fragment in the path.
stringgetState
unknownReports the current state of the location history.
unknownisCurrentPathEqualTo
booleanNormalizes the given path and compares to the current normalized path.
stringThe given URL path.
stringQuery parameters.
booleannormalize
stringNormalizes a URL path by stripping any trailing slashes.
stringString representing a URL.
stringprepareExternalUrl
stringNormalizes an external URL path.
If the given URL doesn't begin with a leading slash ('/'), adds one
before normalizing. Adds a hash if HashLocationStrategy is
in use, or the APP_BASE_HREF if the PathLocationStrategy is in use.
stringString representing a URL.
stringgo
voidChanges the browser's URL to a normalized version of a given URL, and pushes a new item onto the platform's history.
stringURL path to normalize.
stringQuery parameters.
anyLocation history state.
voidreplaceState
voidChanges the browser's URL to a normalized version of the given URL, and replaces the top item on the platform's history stack.
stringURL path to normalize.
stringQuery parameters.
anyLocation history state.
voidforward
voidNavigates forward in the platform's history.
voidback
voidNavigates back in the platform's history.
voidhistoryGo
voidNavigate to a specific page from session history, identified by its relative position to the current page.
numberPosition of the target page in the history relative to the current
page.
A negative value moves backwards, a positive value moves forwards, e.g. location.historyGo(2)
moves forward two pages and location.historyGo(-2) moves back two pages. When we try to go
beyond what's stored in the history session, we stay in the current page. Same behaviour occurs
when relativePosition equals 0.
voidonUrlChange
VoidFunctionRegisters a URL change listener. Use to catch updates performed by the Angular framework that are not detectible through "popstate" or "hashchange" events.
(url: string, state: unknown) => voidThe change handler function, which take a URL and a location history state.
VoidFunctionsubscribe
SubscriptionLikeSubscribes to the platform's popState events.
Note: Location.go() does not trigger the popState event in the browser. Use
Location.onUrlChange() to subscribe to URL changes instead.
((exception: any) => void) | null | undefined(() => void) | null | undefinedSubscriptionLikenormalizeQueryParams
(params: string) => stringNormalizes URL parameters by prepending with ? if needed.
joinWithSlash
(start: string, end: string) => stringJoins two parts of a URL with a slash if needed.
stripTrailingSlash
(url: string) => stringRemoves a trailing slash from a URL string if needed.
Looks for the first occurrence of either #, ?, or the end of the
line as / characters and removes the trailing slash if one exists.
Description
A service that applications can use to interact with a browser's URL.
Depending on the LocationStrategy used, Location persists
to the URL's path or the URL's hash segment.
Usage Notes
It's better to use the Router.navigate() service to trigger route changes. Use
Location only if you need to interact with or create normalized URLs outside of
routing.
Location is responsible for normalizing the URL against the application's base href.
A normalized URL is absolute from the URL host, includes the application's base href, and has no
trailing slash:
/my/app/user/123is normalizedmy/app/user/123is not normalized/my/app/user/123/is not normalized
Example
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';import {Component} from '@angular/core';@Component({ selector: 'path-location', providers: [Location, {provide: LocationStrategy, useClass: PathLocationStrategy}], template: ` <h1>PathLocationStrategy</h1> Current URL is: <code>{{ location.path() }}</code ><br /> Normalize: <code>/foo/bar/</code> is: <code>{{ location.normalize('foo/bar') }}</code ><br /> `, standalone: false,})export class PathLocationComponent { location: Location; constructor(location: Location) { this.location = location; }}