# Class UrlService

API for URL management

- Defined in router/url/url-service.js:20

#####
Index

### Constructors

constructor

### Properties

$browser
$get
$location
$locationProvider
_baseHref
_stopListeningFn
_urlListeners
config
hash
interceptDeferred
location
paramFactory
path
rules
search
stateService
urlRuleFactory
$inject

### Methods

baseHref
compile
deferIntercept
href
html5Mode
isMatcher
listen
match
onChange
parts
push
sync
update
url

##
Constructors

### constructor

- new UrlService($locationProvider, stateService, globals, urlConfigProvider): UrlService

- #### Parameters

  - $locationProvider: $LocationProvider
  - stateService: any
  - globals: any
  - urlConfigProvider: any

  #### Returns UrlService

  - Defined in router/url/url-service.js:31

##
Properties

### $browser

$browser: Browser

- Defined in router/url/url-service.js:37
- Defined in router/url/url-service.js:104

### $get

$get: (string | (($location: Location, $browser: Browser, $rootScope: Scope) => UrlService))[] = ...

- Defined in router/url/url-service.js:91

### $location

$location: Location

- Defined in router/url/url-service.js:36
- Defined in router/url/url-service.js:103

### $locationProvider

$locationProvider: $LocationProvider

- Defined in router/url/url-service.js:34

### _baseHref

_baseHref: string

- Defined in router/url/url-service.js:122

### _stopListeningFn

_stopListeningFn: any

- Defined in router/url/url-service.js:287

### _urlListeners

_urlListeners: any[]

- Defined in router/url/url-service.js:88

### config

config: UrlConfigProvider

The nested [[UrlConfig]] API to configure the URL and retrieve URL information

See: [[UrlConfig]] for details

- Defined in router/url/url-service.js:58

### hash

hash: (() => string | Location)

Gets the hash part of the current url

If the current URL is `/some/path?query=value#anchor`, this returns `anchor`

#### Type declaration

- - (): string | Location

  - #### Returns string | Location

    the hash (anchor) portion of the url

- Defined in router/url/url-service.js:86

### interceptDeferred

interceptDeferred: boolean

- Defined in router/url/url-service.js:40
- Defined in router/url/url-service.js:317

### location

location: string | Location

- Defined in router/url/url-service.js:356

### paramFactory

paramFactory: ParamFactory

Creates a new [[Param]] for a given location (DefType)

- Defined in router/url/url-service.js:61

### path

path: (() => any)

Gets the path part of the current url

If the current URL is `/some/path?query=value#anchor`, this returns `/some/path`

#### Type declaration

- - (): any

  - #### Returns any

    the path portion of the url

- Defined in router/url/url-service.js:70

### rules

rules: UrlRules

The nested [[UrlRules]] API for managing URL rules and rewrites

See: [[UrlRules]] for details

- Defined in router/url/url-service.js:51

### search

search: (() => any)

Gets the search part of the current url as an object

If the current URL is `/some/path?query=value#anchor`, this returns `{ query: 'value' }`

#### Type declaration

- - (): any

  - #### Returns any

    the search (query) portion of the url, as an object

- Defined in router/url/url-service.js:78

### stateService

stateService: any

- Defined in router/url/url-service.js:32

### urlRuleFactory

urlRuleFactory: UrlRuleFactory

Provides services related to the URL

- Defined in router/url/url-service.js:43

### `Static`$inject

$inject: string[] = ...

- Defined in router/url/url-service.js:21

##
Methods

### baseHref

- baseHref(): string

- #### Returns string

  - Defined in router/url/url-service.js:119

### compile

- compile(pattern, config): UrlMatcher

- Creates a [[UrlMatcher]] for the specified pattern.

  #### Parameters

  - pattern: any

    The URL pattern.

  - config: any

    The config object hash.

  #### Returns UrlMatcher

  The UrlMatcher.

  - Defined in router/url/url-service.js:429

### deferIntercept

- deferIntercept(defer): void

- Disables monitoring of the URL.

  Call this method before ng-router has bootstrapped. It will stop ng-router from performing the initial url sync.

  This can be useful to perform some asynchronous initialization before the router starts. Once the initialization is complete, call [[listen]] to tell ng-router to start watching and synchronizing the URL.

  #### Example:

      // Prevent ng-router from automatically intercepting URL changes when it starts;
      urlService.deferIntercept();

      fetch('/states.json').then(resp => resp.json()).then(data => {
        data.forEach(state => $stateRegistry.register(state));
        urlService.listen();
        urlService.sync();
      });

  #### Parameters

  - defer: any

    Indicates whether to defer location change interception. Passing no parameter is equivalent to `true`.

  #### Returns void

  - Defined in router/url/url-service.js:315

### href

- href(urlMatcher, params, options): any

- Builds and returns a URL with interpolated parameters

  #### Example:

      matcher = $umf.compile("/about/:person");
      params = { person: "bob" };
      $bob = $urlService.href(matcher, params);
      // $bob == "/about/bob";

  #### Parameters

  - urlMatcher: any

    The [[UrlMatcher]] object which is used as the template of the URL to generate.

  - params: any

    An object of parameter values to fill the matcher's required parameters.

  - options: any

    Options object. The options are:

    - `absolute` - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl".

  #### Returns any

  Returns the fully compiled URL, or `null` if `params` fail validation against `urlMatcher`

  - Defined in router/url/url-service.js:397

### html5Mode

- html5Mode(): boolean

- #### Returns boolean

  - Defined in router/url/url-service.js:113

### isMatcher

- isMatcher(object): boolean

- Returns true if the specified object is a [[UrlMatcher]], or false otherwise.

  #### Parameters

  - object: any

    The object to perform the type check against.

  #### Returns boolean

  `true` if the object matches the `UrlMatcher` interface, by implementing all the same methods.

  - Defined in router/url/url-service.js:453

### listen

- listen(enabled): any

- Starts or stops listening for URL changes

  Call this sometime after calling [[deferIntercept]] to start monitoring the url. This causes ng-router to start listening for changes to the URL, if it wasn't already listening.

  If called with `false`, ng-router will stop listening (call listen(true) to start listening again).

  #### Example:

      urlService.deferIntercept();

      fetch('/states.json').then(resp => resp.json()).then(data => {
        data.forEach(state => $stateRegistry.register(state));
        // Start responding to URL changes
        urlService.listen();
        urlService.sync();
      });

  #### Parameters

  - enabled: any

    `true` or `false` to start or stop listening to URL changes

  #### Returns any

  - Defined in router/url/url-service.js:282

### match

- match(url): any

- Matches a URL

  Given a URL (as a [[UrlParts]] object), check all rules and determine the best matching rule. Return the result as a [[MatchResult]].

  #### Parameters

  - url: any

  #### Returns any

  - Defined in router/url/url-service.js:326

### onChange

- onChange(callback): (() => any)
- `Internal`

  Registers a low level url change handler

  Note: Because this is a low level handler, it's not recommended for general use.

  #### Example:

      let deregisterFn = locationServices.onChange((evt) => console.log("url change", evt));

  #### Parameters

  - callback: any

    a function that will be called when the url is changing

  #### Returns (() => any)

  a function that de-registers the callback

  - - (): any

    - #### Returns any

  - Defined in router/url/url-service.js:200

### parts

- parts(): {
  hash: string | Location;
  path: any;
  search: any;
  }

- Gets the current URL parts

  This method returns the different parts of the current URL (the [[path]], [[search]], and [[hash]]) as a [[UrlParts]] object.

  #### Returns {
  hash: string | Location;
  path: any;
  search: any;
  }

  - ##### hash: string | Location

  - ##### path: any

  - ##### search: any

  - Defined in router/url/url-service.js:210

### push

- push(urlMatcher, params, options): void
- `Internal`

  Internal API.

  Pushes a new location to the browser history.

  #### Parameters

  - urlMatcher: any
  - params: any
  - options: any

  #### Returns void

  - Defined in router/url/url-service.js:373

### sync

- sync(evt): void

- Activates the best rule for the current URL

  Checks the current URL for a matching [[UrlRule]], then invokes that rule's handler. This method is called internally any time the URL has changed.

  This effectively activates the state (or redirect, etc) which matches the current URL.

  #### Example:

      urlService.deferIntercept();

      fetch('/states.json').then(resp => resp.json()).then(data => {
        data.forEach(state => $stateRegistry.register(state));
        urlService.listen();
        // Find the matching URL and invoke the handler.
        urlService.sync();
      });

  #### Parameters

  - evt: any

  #### Returns void

  - Defined in router/url/url-service.js:233

### update

- update(read): void

- #### Parameters

  - read: any

  #### Returns void

  - Defined in router/url/url-service.js:354

### url

- url(newUrl?, replace?, state?): string | Location

- Gets the current url, or updates the url

  ### Getting the current URL

  When no arguments are passed, returns the current URL. The URL is normalized using the internal [[path]]/[[search]]/[[hash]] values.

  For example, the URL may be stored in the hash ([[HashLocationServices]]) or have a base HREF prepended ([[PushStateLocationServices]]).

  The raw URL in the browser might be:

      http://mysite.com/somepath/index.html#/internal/path/123?param1=foo#anchor

  or

      http://mysite.com/basepath/internal/path/123?param1=foo#anchor

  then this method returns:

      /internal/path/123?param1=foo#anchor

  #### Example:

      locationServices.url(); // "/some/path?query=value#anchor"

  ### Updating the URL

  When `newurl` arguments is provided, changes the URL to reflect `newurl`

  #### Example:

      locationServices.url("/some/path?query=value#anchor", true);

  #### Parameters

  - `Optional` newUrl: string

    The new value for the URL. This url should reflect only the new internal [[path]], [[search]], and [[hash]] values. It should not include the protocol, site, port, or base path of an absolute HREF.

  - `Optional` replace: boolean = false

    When true, replaces the current history entry (instead of appending it) with this new url

  - `Optional` state: any

    The history's state object, i.e., pushState (if the LocationServices implementation supports it)

  #### Returns string | Location

  the url (after potentially being processed)

  - Defined in router/url/url-service.js:178