Location

class

npm Package @angular/common
Module import { Location } from '@angular/common';
Source common/src/location/location.ts

Location is a service that applications can use to interact with a browser's URL.

Overview

class Location {
  constructor(platformStrategy: LocationStrategy)
  static normalizeQueryParams(params: string): string
  static joinWithSlash(start: string, end: string): string
  static stripTrailingSlash(url: string): string
  path(includeHash: boolean = false): string
  isCurrentPathEqualTo(path: string, query: string = ''): boolean
  normalize(url: string): string
  prepareExternalUrl(url: string): string
  go(path: string, query: string = ''): void
  replaceState(path: string, query: string = ''): void
  forward(): void
  back(): void
  subscribe(onNext: (value: PopStateEvent) => void, onThrow?: ((exception: any) => void)|null, onReturn?: (() => void)|null): Object
}

Description

Depending on which LocationStrategy is used, Location will either persist to the URL's path or the URL's hash segment.

Note: it's better to use Router 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/123 is normalized
  • my/app/user/123 is 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>
  `
})
export class PathLocationComponent {
  location: Location;
  constructor(location: Location) { this.location = location; }
}

Subclasses

  • SpyLocation

Static Members

static normalizeQueryParams(params: string): string

Given a string of url parameters, prepend with '?' if needed, otherwise return parameters as is.

static joinWithSlash(start: string, end: string): string

Given 2 parts of a url, join them with a slash if needed.

static stripTrailingSlash(url: string): string

If url has a trailing slash, remove it, otherwise return url as is. This method looks for the first occurence of either #, ?, or the end of the line as / characters after any of these should not be replaced.

Constructor

constructor(platformStrategy: LocationStrategy)

Members

path(includeHash: boolean = false): string

Returns the normalized URL path.

isCurrentPathEqualTo(path: string, query: string = ''): boolean

Normalizes the given path and compares to the current normalized path.

normalize(url: string): string

Given a string representing a URL, returns the normalized URL path without leading or trailing slashes.

prepareExternalUrl(url: string): string

Given a string representing a URL, returns the platform-specific external URL path. If the given URL doesn't begin with a leading slash ('/'), this method adds one before normalizing. This method will also add a hash if HashLocationStrategy is used, or the APP_BASE_HREF if the PathLocationStrategy is in use.

go(path: string, query: string = ''): void

Changes the browsers URL to the normalized version of the given URL, and pushes a new item onto the platform's history.

replaceState(path: string, query: string = ''): void

Changes the browsers URL to the normalized version of the given URL, and replaces the top item on the platform's history stack.

forward(): void

Navigates forward in the platform's history.

back(): void

Navigates back in the platform's history.

subscribe(onNext: (value: PopStateEvent) => void, onThrow?: ((exception: any) => void)|null, onReturn?: (() => void)|null): Object

Subscribe to the platform's popState events.

Annotations

@Injectable()

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v4.angular.io/api/common/Location