Improve this Doc View Source

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: Using $location

## Dependencies

- `$rootElement`

## Methods

- ###

  `absUrl();`

  This method is getter only.

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

      // given url http://example.com/#/some/path?foo=bar&baz=xoxo
      var absUrl = $location.absUrl();
      // => "http://example.com/#/some/path?foo=bar&baz=xoxo"

  #### Returns

  string | full url

- ###

  `url([url]);`

  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`.

      // given url http://example.com/#/some/path?foo=bar&baz=xoxo
      var url = $location.url();
      // => "/some/path?foo=bar&baz=xoxo"

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  url (optional) | string | New url without base prefix (e.g. `/path?a=b#hash`)

  #### Returns

  string | url

- ###

  `protocol();`

  This method is getter only.

  Return protocol of current url.

      // given url http://example.com/#/some/path?foo=bar&baz=xoxo
      var protocol = $location.protocol();
      // => "http"

  #### Returns

  string | protocol of current url

- ###

  `host();`

  This method is getter only.

  Return host of current url.

      // given url http://example.com/#/some/path?foo=bar&baz=xoxo
      var host = $location.host();
      // => "example.com"

  #### Returns

  string | host of current url.

- ###

  `port();`

  This method is getter only.

  Return port of current url.

      // given url http://example.com/#/some/path?foo=bar&baz=xoxo
      var port = $location.port();
      // => 80

  #### Returns

  Number | port

- ###

  `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.

      // given url http://example.com/#/some/path?foo=bar&baz=xoxo
      var path = $location.path();
      // => "/some/path"

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  path (optional) | string number | New path

  #### Returns

  string | path

- ###

  `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`.

      // given url http://example.com/#/some/path?foo=bar&baz=xoxo
      var searchObject = $location.search();
      // => {foo: 'bar', baz: 'xoxo'}

      // set foo to 'yipee'
      $location.search('foo', 'yipee');
      // $location.search() => {foo: 'yipee', baz: 'xoxo'}

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  search | string Object.<string> Object.<Array.<string>> | New search params - string or hash object. When called with a single argument the method acts as a setter, setting the `search` component of `$location` to the specified value. If the argument is a hash object containing an array of values, these values will be encoded as duplicate search parameters in the url.

  paramValue
  (optional)

  string Number Array.<string> boolean

  If `search` is a string or number, then `paramValue` will override only a single search property.

  If `paramValue` is an array, it will override the property of the `search` component of `$location` specified via the first argument.

  If `paramValue` is `null`, the property specified via the first argument will be deleted.

  If `paramValue` is `true`, the property specified via the first argument will be added with no value nor trailing equal sign.

  #### Returns

  Object | If called with no arguments returns the parsed `search` object. If called with one or more arguments returns `$location` object itself.

- ###

  `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`.

      // given url http://example.com/some/path?foo=bar&baz=xoxo#hashValue
      var hash = $location.hash();
      // => "hashValue"

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  hash (optional) | string number | New hash fragment

  #### Returns

  string | hash

- ###

  `replace();`

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

- ###

  `state([state]);`

  This method is getter / setter.

  Return the history state object when called without any parameter.

  Change the history state object when called with one parameter and return `$location`. The state object is later passed to `pushState` or `replaceState`.

  NOTE: This method is supported only in HTML5 mode and only in browsers supporting the HTML5 History API (i.e. methods `pushState` and `replaceState`). If you need to support older browsers (like IE9 or Android < 4.0), don't use this method.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  state (optional) | object | State object for pushState or replaceState

  #### Returns

  object | state

## Events

- ### $locationChangeStart

  Broadcasted before a URL will change.

  This change can be prevented by calling `preventDefault` method of the event. See `$rootScope.Scope` for more details about event object. Upon successful change $locationChangeSuccess is fired.

  The `newState` and `oldState` parameters may be defined only in HTML5 mode and when the browser supports the HTML5 History API.

  #### Type:

  broadcast

  #### Target:

  root scope

- ### $locationChangeSuccess

  Broadcasted after a URL was changed.

  The `newState` and `oldState` parameters may be defined only in HTML5 mode and when the browser supports the HTML5 History API.

  #### Type:

  broadcast

  #### Target:

  root scope