View source Improve this doc

$location

service in module ng

Description

The $location service parses the URL in the browser address bar (based on the window.location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.

The $location service:

  • Exposes the current URL in the browser address bar, so you can
    • Watch and observe the URL.
    • Change the URL.
  • Synchronizes the URL with the browser when the user
    • Changes the address bar.
    • Clicks the back or forward button (or clicks a History link).
    • Clicks on a link.
  • Represents the URL object as a set of methods (protocol, host, port, path, search, hash).

For more information see Developer Guide: Angular Services: Using $location

Dependencies

  • $browser
  • $sniffer
  • $rootElement

Methods

  • absUrl()

This method is getter only.

Return full url representation with all segments encoded according to rules specified in RFC 3986.

Returns

string

full url

  • hash(hash)

This method is getter / setter.

Return hash fragment when called without any parameter.

Change hash fragment when called with parameter and return $location.

Parameters

ParamTypeDetailshash (optional) string

New hash fragment

Returns

string

hash

  • host()

This method is getter only.

Return host of current url.

Returns

string

host of current url.

  • path(path)

This method is getter / setter.

Return path of current url when called without any parameter.

Change path when called with parameter and return $location.

Note: Path should always begin with forward slash (/), this method will add the forward slash if it is missing.

Parameters

ParamTypeDetailspath (optional) string

New path

Returns

string

path

  • port()

This method is getter only.

Return port of current url.

Returns

Number

port

  • protocol()

This method is getter only.

Return protocol of current url.

Returns

string

protocol of current url

  • replace()

If called, all changes to $location during current $digest will be replacing current history record, instead of adding new one.

  • search(search, paramValue)

This method is getter / setter.

Return search part (as object) of current url when called without any parameter.

Change search part when called with parameter and return $location.

Parameters

ParamTypeDetailssearchstringObject.Object.>

New search params - string or hash object. Hash object may contain an array of values, which will be decoded as duplicates in the url.

paramValue (optional) stringArray

If search is a string, then paramValue will override only a single search parameter. If paramValue is an array, it will set the parameter as a comma-separated value. If paramValue is null, the parameter will be deleted.

Returns

string

search

  • url(url, replace)

This method is getter / setter.

Return url (e.g. /path?a=b#hash) when called without any parameter.

Change path, search and hash, when called with parameter and return $location.

Parameters

ParamTypeDetailsurl (optional) string

New url without base prefix (e.g. /path?a=b#hash)

replace (optional) string

The path that will be changed

Returns

string

url

Events

  • $locationChangeStart

Broadcasted before a URL will change. This change can be prevented by calling preventDefault method of the event. See ng.$rootScope.Scope#$on for more details about event object. Upon successful change $locationChangeSuccess is fired.

Type:

broadcast

Target:

root scope

Parameters

ParamTypeDetailsangularEventObject

Synthetic event object.

newUrlstring

New URL

oldUrl (optional) string

URL that was before it was changed.

  • $locationChangeSuccess

Broadcasted after a URL was changed.

Type:

broadcast

Target:

root scope

Parameters

ParamTypeDetailsangularEventObject

Synthetic event object.

newUrlstring

New URL

oldUrl (optional) string

URL that was before it was changed.