StateService | UI-Router
Options
All
  • Public
  • Public/Protected
  • All
Menu

Class StateService

Hierarchy

  • StateService

Index

Properties

router: UIRouter

Accessors

Methods

  • Handler for when transitionTo is called with an invalid state.

    Invokes the onInvalid callbacks, in natural order. Each callback's return value is checked in sequence until one of them returns an instance of TargetState. The results of the callbacks are wrapped in $q.when(), so the callbacks may return promises.

    If a callback returns an TargetState, then it is used as arguments to $state.transitionTo() and the result returned.

    Parameters

    Returns any


  • defaultErrorHandler(handler?: function): function
  • Sets or gets the default transitionTo error handler.

  • Sets or gets the default transitionTo error handler.

    The error handler is called when a Transition is rejected or when any error occurred during the Transition. This includes errors caused by resolves and transition hooks.

    The built-in default error handler logs the error to the console.

    You can provide your own custom handler.

    example
    
    stateService.defaultErrorHandler(function() {
      // Do not log transitionTo errors
    });
    

    Parameters

    • handler Optional: function
      :

      a global error handler function

        • (error: any): void
        • Parameters

          • error: any

          Returns void

    Returns function

    :

    the current global error handler

      • (error: any): void
      • Parameters

        • error: any

        Returns void


  • ngdoc

    function

    name

    ui.router.state.$state#go

    methodof

    ui.router.state.$state

    description

    Convenience method for transitioning to a new state. $state.go calls $state.transitionTo internally but automatically sets options to { location: true, inherit: true, relative: $state.$current, notify: true }. This allows you to easily use an absolute or relative to path and specify only the parameters you'd like to update (while letting unspecified parameters inherit from the currently active ancestor states).

    example

    let app = angular.module('app', ['ui.router']);

    app.controller('ctrl', function ($scope, $state) { $scope.changeState = function () { $state.go('contact.detail'); }; });

    Parameters

    • to: StateOrName
      :

      Absolute state name or relative state path. Some examples:

      • $state.go('contact.detail') - will go to the contact.detail state
      • $state.go('^') - will go to a parent state
      • $state.go('^.sibling') - will go to a sibling state
      • $state.go('.child.grandchild') - will go to grandchild state
    • params Optional: RawParams
      :

      A map of the parameters that will be sent to the state, will populate $stateParams. Any parameters that are not specified will be inherited from currently defined parameters. This allows, for example, going to a sibling state that shares parameters specified in a parent state. Parameter inheritance only works between common ancestor states, I.e. transitioning to a sibling will get you the parameters for all parents, transitioning to a child will get you all current parameters, etc.

    • options Optional: TransitionOptions
      :

      Options object. The options are:

      • location - {boolean=true|string=} - If true will update the url in the location bar, if false will not. If string, must be "replace", which will update url and also replace last history record.
      • inherit - {boolean=true}, If true will inherit url parameters from current url.
      • relative - {object=$state.$current}, When transitioning with relative path (e.g '^'), defines which state to be relative from.
      • notify - {boolean=true}, If true will broadcast $stateChangeStart and $stateChangeSuccess events.
      • reload (v0.2.5) - {boolean=false}, If true will force transition even if the state or params have not changed, aka a reload of the same state. It differs from reloadOnSearch because you'd use this when you want to force a reload when everything is the same, including search params.

    Returns TransitionPromise

    :

    A promise representing the state of the new transition.

    Possible success values:

    • $state.current


    Possible rejection values:

    • 'transition superseded' - when a newer transition has been started after this one
    • 'transition prevented' - when event.preventDefault() has been called in a $stateChangeStart listener
    • 'transition aborted' - when event.preventDefault() has been called in a $stateNotFound listener or when a $stateNotFound event.retry promise errors.
    • 'transition failed' - when a state has been unsuccessfully found after 2 tries.
    • resolve error - when an error has occurred with a resolve

  • ngdoc

    function

    name

    ui.router.state.$state#href

    methodof

    ui.router.state.$state

    description

    A url generation method that returns the compiled url for the given state populated with the given params.

    example
    expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
    

    Parameters

    • stateOrName: StateOrName
      :

      The state name or state object you'd like to generate a url from.

    • params Optional: RawParams
      :

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

    • options Optional: HrefOptions
      :

      Options object. The options are:

      • lossy - {boolean=true} - If true, and if there is no url associated with the state provided in the first parameter, then the constructed href url will be built from the first navigable ancestor (aka ancestor with a valid url).
      • inherit - {boolean=true}, If true will inherit url parameters from current url.
      • relative - {object=$state.$current}, When transitioning with relative path (e.g '^'), defines which state to be relative from.
      • absolute - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl".

    Returns string

    :

    compiled state url


  • ngdoc

    function

    name

    ui.router.state.$state#includes

    methodof

    ui.router.state.$state

    description

    A method to determine if the current active state is equal to or is the child of the state stateName. If any params are passed then they will be tested for a match as well. Not all the parameters need to be passed, just the ones you'd like to test for equality.

    example

    Partial and relative names

    $state.$current.name = 'contacts.details.item';
    
    // Using partial names
    $state.includes("contacts"); // returns true
    $state.includes("contacts.details"); // returns true
    $state.includes("contacts.details.item"); // returns true
    $state.includes("contacts.list"); // returns false
    $state.includes("about"); // returns false
    
    // Using relative names (. and ^), typically from a template
    // E.g. from the 'contacts.details' template
    
    Item

    Basic globbing patterns

    $state.$current.name = 'contacts.details.item.url';
    
    $state.includes("*.details.*.*"); // returns true
    $state.includes("*.details.**"); // returns true
    $state.includes("**.item.**"); // returns true
    $state.includes("*.details.item.url"); // returns true
    $state.includes("*.details.*.url"); // returns true
    $state.includes("*.details.*"); // returns false
    $state.includes("item.**"); // returns false
    

    Parameters

    • stateOrName: StateOrName
      :

      A partial name, relative name, or glob pattern to be searched for within the current state name.

    • params Optional: RawParams
      :

      A param object, e.g. {sectionId: section.id}, that you'd like to test against the current active state.

    • options Optional: TransitionOptions
      :

      An options object. The options are:

      • relative - {string|object=} - If stateOrName is a relative state reference and options.relative is set, .includes will test relative to options.relative state (or name).

    Returns boolean

    :

    Returns true if it does include the state


  • ngdoc

    function

    name

    ui.router.state.$state#is

    methodof

    ui.router.state.$state

    description

    Similar to {@link ui.router.state.$state#methods_includes $state.includes}, but only checks for the full state name. If params is supplied then it will be tested for strict equality against the current active params object, so all params must match with none missing and no extras.

    example
    $state.$current.name = 'contacts.details.item';
    
    // absolute name
    $state.is('contact.details.item'); // returns true
    $state.is(contactDetailItemStateObject); // returns true
    
    // relative name (. and ^), typically from a template
    // E.g. from the 'contacts.details' template
    
    Item

    Parameters

    • stateOrName: StateOrName
      :

      The state name (absolute or relative) or state object you'd like to check.

    • params Optional: RawParams
      :

      A param object, e.g. {sectionId: section.id}, that you'd like to test against the current active state.

    • options Optional: TransitionOptions
      :

      An options object. The options are:

      • relative - {string|object} - If stateOrName is a relative state name and options.relative is set, .is will test relative to options.relative state (or name).

    Returns boolean

    :

    Returns true if it is the state.


  • ngdoc

    function

    name

    ui.router.state.$state#reload

    methodof

    ui.router.state.$state

    description

    A method that force reloads the current state, or a partial state hierarchy. All resolves are re-resolved, controllers reinstantiated, and events re-fired.

    example
    let app angular.module('app', ['ui.router']);
    
    app.controller('ctrl', function ($scope, $state) {
      $scope.reload = function(){
        $state.reload();
      }
    });
    

    reload() is just an alias for:

    $state.transitionTo($state.current, $stateParams, {
      reload: true, inherit: false, notify: true
    });
    
    example
    //assuming app application consists of 3 states: 'contacts', 'contacts.detail', 'contacts.detail.item'
    //and current state is 'contacts.detail.item'
    let app angular.module('app', ['ui.router']);
    
    app.controller('ctrl', function ($scope, $state) {
      $scope.reload = function(){
        //will reload 'contact.detail' and nested 'contact.detail.item' states
        $state.reload('contact.detail');
      }
    });
    

    Parameters

    • reloadState: StateOrName
      :

      A state name or a state object, which is the root of the resolves to be re-resolved.

    Returns Promise<State>

    :

    A promise representing the state of the new transition. See {@link ui.router.state.$state#methods_go $state.go}.


  • ngdoc

    function

    name

    ui.router.state.$state#transitionTo

    methodof

    ui.router.state.$state

    description

    Low-level method for transitioning to a new state. {@link ui.router.state.$state#methods_go $state.go} uses transitionTo internally. $state.go is recommended in most situations.

    example
    let app = angular.module('app', ['ui.router']);
    
    app.controller('ctrl', function ($scope, $state) {
      $scope.changeState = function () {
        $state.transitionTo('contact.detail');
      };
    });
    

    Parameters

    • to: StateOrName
      :

      State name.

    • toParams Default value: RawParams = {}
      :

      A map of the parameters that will be sent to the state, will populate $stateParams.

    • options Default value: TransitionOptions = {}
      :

      Options object. The options are:

      • location - {boolean=true|string=} - If true will update the url in the location bar, if false will not. If string, must be "replace", which will update url and also replace last history record.
      • inherit - {boolean=false}, If true will inherit url parameters from current url.
      • relative - {object=}, When transitioning with relative path (e.g '^'), defines which state to be relative from.
      • notify - {boolean=true}, If true will broadcast $stateChangeStart and $stateChangeSuccess events.
      • reload (v0.2.5) - {boolean=false}, If true will force transition even if the state or params have not changed, aka a reload of the same state. It differs from reloadOnSearch because you'd use this when you want to force a reload when everything is the same, including search params.

    Returns TransitionPromise

    :

    A promise representing the state of the new transition. See {@link ui.router.state.$state#methods_go $state.go}.


Generated using TypeDoc