A service that applications can use to interact with a browser's URL.
API
class Location implements OnDestroy {
constructor(locationStrategy: LocationStrategy): Location;
path(includeHash?: boolean): string;
getState(): unknown;
isCurrentPathEqualTo(path: string, query?: string): boolean;
normalize(url: string): string;
prepareExternalUrl(url: string): string;
go(path: string, query?: string, state?: any): void;
replaceState(path: string, query?: string, state?: any): void;
forward(): void;
back(): void;
historyGo(relativePosition?: number): void;
onUrlChange(fn: (url: string, state: unknown) => void): VoidFunction;
subscribe(onNext: (value: PopStateEvent) => void, onThrow?: ((exception: any) => void) | null | undefined, onReturn?: (() => void) | null | undefined): SubscriptionLike;
static normalizeQueryParams: (params: string) => string;
static joinWithSlash: (start: string, end: string) => string;
static stripTrailingSlash: (url: string) => string;
}
path
stringNormalizes the URL path for this location.
booleanTrue to include an anchor fragment in the path.
string
getState
unknownReports the current state of the location history.
unknown
isCurrentPathEqualTo
booleanNormalizes the given path and compares to the current normalized path.
stringThe given URL path.
stringQuery parameters.
boolean
normalize
stringNormalizes a URL path by stripping any trailing slashes.
stringString representing a URL.
string
prepareExternalUrl
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.
string
go
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.
void
replaceState
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.
void
forward
voidNavigates forward in the platform's history.
void
back
voidNavigates back in the platform's history.
void
historyGo
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.
void
onUrlChange
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.
VoidFunction
subscribe
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 | undefined
SubscriptionLike
normalizeQueryParams
(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 normalized -
my/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;
}
}