# angular.service.$location

Work in Progress This page is currently being revised. It might be incomplete or contain inaccuracies.

## Description

Parses the browser location url and makes it available to your application. Any changes to the url are reflected into `$location` service and changes to `$location` are reflected in the browser location url.

Notice that using browser's forward/back buttons changes the $location.

## Dependencies

- `$browser`

## Methods

- ### update(href)

  Updates the location object.

  Does not immediately update the browser. Instead the browser is updated at the end of $eval() cycle.

             $location.update('http://www.angularjs.org/path#hash?search=x');
             $location.update({host: 'www.google.com', protocol: 'https'});
             $location.update({hashPath: '/path', hashSearch: {a: 'b', x: true}});

  #### Parameters

  - `href – {string|Object} –`

    Full href as a string or object with properties

- ### updateHash(path, search)

  Updates the hash fragment part of the url.

  #### Parameters

  - `path – {string|Object} –`

    A hashPath or hashSearch object

  - `search(optional) – {Object} –`

    A hashSearch object

## Properties

- ### href

  The full URL of the current location.

- ### protocol

  The protocol part of the URL (e.g. http or https).

- ### host

  The host name, ip address or FQDN of the current location.

- ### port

  The port number of the current location (e.g. 80, 443, 8080).

- ### path

  The path of the current location (e.g. /myapp/inbox).

- ### search

  Map of query parameters (e.g. {user:"foo", page:23}).

- ### hash

  The fragment part of the URL of the current location (e.g. #foo).

- ### hashPath

  Similar to `path`, but located in the `hash` fragment (e.g. ../foo#/some/path => /some/path).

- ### hashSearch

  Similar to `search` but located in `hash` fragment (e.g. .../foo#/some/path?hashQuery=param => {hashQuery: "param"}).

## Example

       <div ng:init="$location = $service('$location')">
         <a id="ex-test" href="#myPath?name=misko">test hash</a>|
         <a id="ex-reset" href="#!/api/angular.service.$location">reset hash</a><br/>
         <input type='text' name="$location.hash" size="30">
         <pre>$location = {{$location}}</pre>
       </div>

       it('should initialize the input field', function() {
         expect(using('.doc-example-live').input('$location.hash').val()).
           toBe('!/api/angular.service.$location');
       });

       it('should bind $location.hash to the input field', function() {
         using('.doc-example-live').input('$location.hash').enter('foo');
         expect(browser().location().hash()).toBe('foo');
       });

       it('should set the hash to a test string with test link is presed', function() {
         using('.doc-example-live').element('#ex-test').click();
         expect(using('.doc-example-live').input('$location.hash').val()).
           toBe('myPath?name=misko');
       });

       it('should reset $location when reset link is pressed', function() {
         using('.doc-example-live').input('$location.hash').enter('foo');
         using('.doc-example-live').element('#ex-reset').click();
         expect(using('.doc-example-live').input('$location.hash').val()).
           toBe('!/api/angular.service.$location');
       });