Transition | @uirouter/angularjs
Options
Menu

Represents a transition between two states.

When navigating to a state, we are transitioning from the current state to the new state.

This object contains all contextual information about the to/from states, parameters, resolves. It has information about all states being entered and exited as a result of the transition.

Hierarchy

  • Transition

Implements

Index

Constructors

  • Creates a new Transition object.

    If the target state is not valid, an error is thrown.

    Parameters

    • fromPath PathNode[]
      :

      The path of PathNodes from which the transition is leaving. The last node in the fromPath encapsulates the "from state".

    • targetState TargetState
      :

      The target state and parameters being transitioned to (also, the transition options)

    • router UIRouter
      :

      The UIRouter instance

    Returns Transition


Properties

$id: number

A unique identifier for the transition.

A unique identifier for the transition.

This is an auto incrementing integer, starting from 0.

promise: Promise<any> = this._deferred.promise

This promise is resolved or rejected based on the outcome of the Transition.

This promise is resolved or rejected based on the outcome of the Transition.

When the transition is successful, the promise is resolved When the transition is unsuccessful, the promise is rejected with the Rejection or javascript error

router: UIRouter

A reference to the UIRouter instance

A reference to the UIRouter instance

This reference can be used to access the router services, such as the StateService

success: boolean

A boolean which indicates if the transition was successful

A boolean which indicates if the transition was successful

After a successful transition, this value is set to true. After an unsuccessful transition, this value is set to false.

The value will be undefined if the transition is not complete

Methods

  • Returns StateObject

    :

    the internal from [State] object


  • Returns StateObject

    :

    the internal to [State] object


  • abort(): void
  • Aborts this transition

  • Aborts this transition

    Imperative API to abort a Transition. This only applies to Transitions that are not yet complete.

    Returns void


  • Dynamically adds a new Resolvable (i.e., StateDeclaration.resolve) to this transition.

    Allows a transition hook to dynamically add a Resolvable to this Transition.

    Use the Transition.injector to retrieve the resolved data in subsequent hooks (UIInjector.get).

    If a state argument is provided, the Resolvable is processed when that state is being entered. If no state is provided then the root state is used. If the given state has already been entered, the Resolvable is processed when any child state is entered. If no child states will be entered, the Resolvable is processed during the onFinish phase of the Transition.

    The state argument also scopes the resolved data. The resolved data is available from the injector for that state and any children states.

    Example:

    transitionService.onBefore({}, transition => {
      transition.addResolvable({
        token: 'myResolve',
        deps: ['MyService'],
        resolveFn: myService => myService.getData()
      });
    });

    Parameters

    Returns void


  • applyViewConfigs(router: UIRouter): void
  • Parameters

    Returns void


  • dynamic(): boolean
  • Returns true if the transition is dynamic.

  • Returns true if the transition is dynamic.

    A transition is dynamic if no states are entered nor exited, but at least one dynamic parameter has changed.

    Returns boolean

    :

    true if the Transition is dynamic


  • entering(): StateDeclaration[]
  • Gets the states being entered.

  • Gets the states being entered.

    Returns StateDeclaration[]

    :

    an array of states that will be entered during this transition.


  • error(): Rejection
  • The Transition error reason.

  • The Transition error reason.

    If the transition is invalid (and could not be run), returns the reason the transition is invalid. If the transition was valid and ran, but was not successful, returns the reason the transition failed.

    Returns Rejection

    :

    a transition rejection explaining why the transition is invalid, or the reason the transition failed.


  • exiting(): StateDeclaration[]
  • Gets the states being exited.

  • Gets the states being exited.

    Returns StateDeclaration[]

    :

    an array of states that will be exited during this transition.


  • Returns the "from state"

    Returns the state that the transition is coming from.

    Returns StateDeclaration

    :

    The state declaration object for the Transition's ("from state").


  • getHooks(hookName: string): RegisteredHook[]
  • Parameters

    • hookName string

    Returns RegisteredHook[]


  • getResolveTokens(pathname?: string): any[]
  • Gets all available resolve tokens (keys)

  • Gets all available resolve tokens (keys)

    This method can be used in conjunction with injector to inspect the resolve values available to the Transition.

    This returns all the tokens defined on StateDeclaration.resolve blocks, for the states in the Transition's TreeChanges.to path.

    Example:

    This example logs all resolve values

    let tokens = trans.getResolveTokens();
    tokens.forEach(token => console.log(token + " = " + trans.injector().get(token)));

    Example:

    This example creates promises for each resolve value. This triggers fetches of resolves (if any have not yet been fetched). When all promises have all settled, it logs the resolve values.

    let tokens = trans.getResolveTokens();
    let promise = tokens.map(token => trans.injector().getAsync(token));
    Promise.all(promises).then(values => console.log("Resolved values: " + values));

    Note: Angular 1 users whould use $q.all()

    Parameters

    • pathname: Default value  string = "to"
      :

      resolve context's path name (e.g., to or from)

    Returns any[]

    :

    an array of resolve tokens (keys)


  • ignored(): boolean
  • Returns true if the transition is ignored.

  • Returns true if the transition is ignored.

    A transition is ignored if no states are entered nor exited, and no parameter values have changed.

    Returns boolean

    :

    true if the Transition is ignored.


  • Creates a UIInjector Dependency Injector

    Returns a Dependency Injector for the Transition's target state (to state). The injector provides resolve values which the target state has access to.

    The UIInjector can also provide values from the native root/global injector (ng1/ng2).

    Example:

    .onEnter({ entering: 'myState' }, trans => {
      var myResolveValue = trans.injector().get('myResolve');
      // Inject a global service from the global/native injector (if it exists)
      var MyService = trans.injector().get('MyService');
    })

    In some cases (such as onBefore), you may need access to some resolve data but it has not yet been fetched. You can use UIInjector.getAsync to get a promise for the data.

    Example:

    .onBefore({}, trans => {
      return trans.injector().getAsync('myResolve').then(myResolveValue =>
        return myResolveValue !== 'ABORT';
      });
    });

    If a state is provided, the injector that is returned will be limited to resolve values that the provided state has access to. This can be useful if both a parent state foo and a child state foo.bar have both defined a resolve such as data.

    Example:

    .onEnter({ to: 'foo.bar' }, trans => {
      // returns result of `foo` state's `myResolve` resolve
      // even though `foo.bar` also has a `myResolve` resolve
      var fooData = trans.injector('foo').get('myResolve');
    });

    If you need resolve data from the exiting states, pass 'from' as pathName. The resolve data from the from path will be returned.

    Example:

    .onExit({ exiting: 'foo.bar' }, trans => {
      // Gets the resolve value of `myResolve` from the state being exited
      var fooData = trans.injector(null, 'from').get('myResolve');
    });

    Parameters

    • state: Optional  StateOrName
      :

      Limits the resolves provided to only the resolves the provided state has access to.

    • pathName: Default value  string = "to"
      :

      Default: 'to': Chooses the path for which to create the injector. Use this to access resolves for exiting states.

    Returns UIInjector

    :

    a UIInjector


  • is(compare: Transition | object): boolean
  • Determines whether two transitions are equivalent.

  • Determines whether two transitions are equivalent.

    deprecated

    Parameters

    Returns boolean


  • isActive(): boolean
  • Checks if this transition is currently active/running.

  • Checks if this transition is currently active/running.

    Returns boolean


  • Registers a TransitionStateHookFn, called when a specific state is entered.

    Registers a lifecycle hook, which is invoked (during a transition) when a specific state is being entered.

    Since this hook is run only when the specific state is being entered, it can be useful for performing tasks when entering a submodule/feature area such as initializing a stateful service, or for guarding access to a submodule/feature area.

    See TransitionStateHookFn for the signature of the function.

    The HookMatchCriteria is used to determine which Transitions the hook should be invoked for. onEnter hooks generally specify { entering: 'somestate' }. To match all Transitions, use an empty criteria object {}.

    Lifecycle

    onEnter hooks are invoked when the Transition is entering a state. States are entered after the onRetain phase is complete. If more than one state is being entered, the parent state is entered first. The registered onEnter hooks for a state are invoked in priority order.

    Note: A built-in onEnter hook with high priority is used to fetch lazy resolve data for states being entered.

    Return value

    The hook's return value can be used to pause, cancel, or redirect the current Transition. See HookResult for more information.

    Inside a state declaration

    Instead of registering onEnter hooks using the TransitionService, you may define an onEnter hook directly on a state declaration (see: StateDeclaration.onEnter).

    Examples

    Audit Log

    This example uses a service to log that a user has entered the admin section of an app. This assumes that there are substates of the "admin" state, such as "admin.users", "admin.pages", etc.

    example
    
    $transitions.onEnter({ entering: 'admin' }, function(transition, state) {
      var AuditService = trans.injector().get('AuditService');
      AuditService.log("Entered " + state.name + " module while transitioning to " + transition.to().name);
    }

    Audit Log (inside a state declaration)

    The onEnter inside this state declaration is syntactic sugar for the previous Audit Log example.

    {
      name: 'admin',
      component: 'admin',
      onEnter: function($transition$, $state$) {
        var AuditService = $transition$.injector().get('AuditService');
        AuditService.log("Entered " + state.name + " module while transitioning to " + transition.to().name);
      }
    }

    Note: A state declaration's onEnter function is injected for Angular 1 only.

    Parameters

    Returns Function

    :

    a function which deregisters the hook.


  • Registers a TransitionHookFn, called after a transition has errored.

    Registers a transition lifecycle hook, which is invoked after a transition has been rejected for any reason.

    See TransitionHookFn for the signature of the function.

    The HookMatchCriteria is used to determine which Transitions the hook should be invoked for. To match all Transitions, use an empty criteria object {}.

    Lifecycle

    The onError hooks are chained off the Transition's promise (see Transition.promise). If a Transition fails, its promise is rejected and the onError hooks are invoked. The onError hooks are invoked in priority order.

    Since these hooks are run after the transition is over, their return value is ignored.

    A transition "errors" if it was started, but failed to complete (for any reason). A non-exhaustive list of reasons a transition can error:

    • A transition was cancelled because a new transition started while it was still running (Transition superseded)
    • A transition was cancelled by a Transition Hook returning false
    • A transition was redirected by a Transition Hook returning a TargetState
    • A Transition Hook or resolve function threw an error
    • A Transition Hook returned a rejected promise
    • A resolve function returned a rejected promise

    To check the failure reason, inspect the return value of Transition.error.

    Note: onError should be used for targeted error handling, or error recovery. For simple catch-all error reporting, use StateService.defaultErrorHandler.

    Return value

    Since the Transition is already completed, the hook's return value is ignored

    Parameters

    Returns Function

    :

    a function which deregisters the hook.


  • Registers a TransitionStateHookFn, called when a specific state is exited.

    Registers a lifecycle hook, which is invoked (during a transition) when a specific state is being exited.

    Since this hook is run only when the specific state is being exited, it can be useful for performing tasks when leaving a submodule/feature area such as cleaning up a stateful service, or for preventing the user from leaving a state or submodule until some criteria is satisfied.

    See TransitionStateHookFn for the signature of the function.

    The HookMatchCriteria is used to determine which Transitions the hook should be invoked for. onExit hooks generally specify { exiting: 'somestate' }. To match all Transitions, use an empty criteria object {}.

    Lifecycle

    onExit hooks are invoked when the Transition is exiting a state. States are exited after any onStart phase is complete. If more than one state is being exited, the child states are exited first. The registered onExit hooks for a state are invoked in priority order.

    Return value

    The hook's return value can be used to pause, cancel, or redirect the current Transition. See HookResult for more information.

    Inside a state declaration

    Instead of registering onExit hooks using the TransitionService, you may define an onExit hook directly on a state declaration (see: StateDeclaration.onExit).

    Note: A state declaration's onExit function is injected for Angular 1 only.

    Parameters

    Returns Function

    :

    a function which deregisters the hook.


  • Registers a TransitionHookFn, called just before a transition finishes.

    Registers a transition lifecycle hook, which is invoked just before a transition finishes. This hook is a last chance to cancel or redirect a transition.

    See TransitionHookFn for the signature of the function.

    The HookMatchCriteria is used to determine which Transitions the hook should be invoked for. To match all Transitions, use an empty criteria object {}.

    Lifecycle

    onFinish hooks are invoked after the onEnter phase is complete. These hooks are invoked just before the transition is "committed". Each hook is invoked in priority order.

    Return value

    The hook's return value can be used to pause, cancel, or redirect the current Transition. See HookResult for more information.

    Parameters

    Returns Function

    :

    a function which deregisters the hook.


  • Registers a TransitionStateHookFn, called when a specific state is retained/kept.

    Registers a lifecycle hook, which is invoked (during a transition) for a specific state that was previously active will remain active (is not being entered nor exited).

    This hook is invoked when a state is "retained" or "kept". It means the transition is coming from a substate of the retained state to a substate of the retained state. This hook can be used to perform actions when the user moves from one substate to another, such as between steps in a wizard.

    The HookMatchCriteria is used to determine which Transitions the hook should be invoked for. onRetain hooks generally specify { retained: 'somestate' }. To match all Transitions, use an empty criteria object {}.

    Lifecycle

    onRetain hooks are invoked after any onExit hooks have been fired. If more than one state is retained, the child states' onRetain hooks are invoked first. The registered onRetain hooks for a state are invoked in priority order.

    Return value

    The hook's return value can be used to pause, cancel, or redirect the current Transition. See HookResult for more information.

    Inside a state declaration

    Instead of registering onRetain hooks using the TransitionService, you may define an onRetain hook directly on a state declaration (see: StateDeclaration.onRetain).

    Note: A state declaration's onRetain function is injected for Angular 1 only.

    Parameters

    Returns Function

    :

    a function which deregisters the hook.


  • Registers a TransitionHookFn, called when a transition starts.

    Registers a transition lifecycle hook, which is invoked as a transition starts running. This hook can be useful to perform some asynchronous action before completing a transition.

    See TransitionHookFn for the signature of the function.

    The HookMatchCriteria is used to determine which Transitions the hook should be invoked for. To match all Transitions, use an empty criteria object {}.

    Lifecycle

    onStart hooks are invoked asynchronously when the Transition starts running. This happens after the onBefore phase is complete. At this point, the Transition has not yet exited nor entered any states. The registered onStart hooks are invoked in priority order.

    Note: A built-in onStart hook with high priority is used to fetch any eager resolve data.

    Return value

    The hook's return value can be used to pause, cancel, or redirect the current Transition. See HookResult for more information.

    Example

    Login during transition

    This example intercepts any transition to a state which requires authentication, when the user is not currently authenticated. It allows the user to authenticate asynchronously, then resumes the transition. If the user did not authenticate successfully, it redirects to the "guest" state, which does not require authentication.

    This example assumes:

    • a state tree where all states which require authentication are children of a parent 'auth' state.
    • MyAuthService.isAuthenticated() synchronously returns a boolean.
    • MyAuthService.authenticate() presents a login dialog, and returns a promise which is resolved or rejected, whether or not the login attempt was successful.

    Example:

    // ng1
    $transitions.onStart( { to: 'auth.**' }, function(trans) {
      var $state = trans.router.stateService;
      var MyAuthService = trans.injector().get('MyAuthService');
    
      // If the user is not authenticated
      if (!MyAuthService.isAuthenticated()) {
    
        // Then return a promise for a successful login.
        // The transition will wait for this promise to settle
    
        return MyAuthService.authenticate().catch(function() {
    
          // If the authenticate() method failed for whatever reason,
          // redirect to a 'guest' state which doesn't require auth.
          return $state.target("guest");
        });
      }
    });

    Parameters

    Returns Function

    :

    a function which deregisters the hook.


  • Registers a TransitionHookFn, called after a successful transition completed.

    Registers a transition lifecycle hook, which is invoked after a transition successfully completes.

    See TransitionHookFn for the signature of the function.

    The HookMatchCriteria is used to determine which Transitions the hook should be invoked for. To match all Transitions, use an empty criteria object {}.

    Lifecycle

    onSuccess hooks are chained off the Transition's promise (see Transition.promise). If the Transition is successful and its promise is resolved, then the onSuccess hooks are invoked. Since these hooks are run after the transition is over, their return value is ignored. The onSuccess hooks are invoked in priority order.

    Return value

    Since the Transition is already completed, the hook's return value is ignored

    Parameters

    Returns Function

    :

    a function which deregisters the hook.


  • Get the transition options

    Returns TransitionOptions

    :

    the options for this Transition.


  • originalTransition(): Transition
  • Gets the original transition in a redirect chain

  • Gets the original transition in a redirect chain

    A transition might belong to a long chain of multiple redirects. This method walks the redirectedFrom chain back to the original (first) transition in the chain.

    Example:

    // states
    registry.register({ name: 'A', redirectTo: 'B' });
    registry.register({ name: 'B', redirectTo: 'C' });
    registry.register({ name: 'C', redirectTo: 'D' });
    registry.register({ name: 'D' });
    
    let transitionA = $state.go('A').transition
    
    $transitions.onSuccess({ to: 'D' }, (trans) => {
      trans.to().name === 'D'; // true
      trans.redirectedFrom().to().name === 'C'; // true
      trans.originalTransition() === transitionA; // true
      trans.originalTransition().to().name === 'A'; // true
    });

    Returns Transition

    :

    The original Transition that started a redirect chain


  • params(pathname?: string): object
  • Gets transition parameter values

  • params<T>(pathname?: string): T
  • Gets transition parameter values

    Returns the parameter values for a transition as key/value pairs. This object is immutable.

    By default, returns the new parameter values (for the "to state").

    Example:

    var toParams = transition.params();

    To return the previous parameter values, supply 'from' as the pathname argument.

    Example:

    var fromParams = transition.params('from');

    Parameters

    • pathname: Optional  string
      :

      the name of the treeChanges path to get parameter values for: ('to', 'from', 'entering', 'exiting', 'retained')

    Returns object

    :

    transition parameter values for the desired path.

    • [paramName: string]: any

  • Type parameters

    • T

    Parameters

    • pathname: Optional  string

    Returns T


  • paramsChanged(): object
  • Gets the new values of any parameters that changed during this transition.

  • paramsChanged<T>(): T
  • Gets the new values of any parameters that changed during this transition.

    Returns any parameter values that have changed during a transition, as key/value pairs.

    • Any parameter values that have changed will be present on the returned object reflecting the new value.
    • Any parameters that not have changed will not be present on the returned object.
    • Any new parameters that weren't present in the "from" state, but are now present in the "to" state will be present on the returned object.
    • Any previous parameters that are no longer present (because the "to" state doesn't have them) will be included with a value of undefined.

    The returned object is immutable.

    Examples:

    Given:

    var stateA = { name: 'stateA', url: '/stateA/:param1/param2' }
    var stateB = { name: 'stateB', url: '/stateB/:param3' }
    var stateC = { name: 'stateB.nest', url: '/nest/:param4' }

    Example 1

    From /stateA/abc/def to /stateA/abc/xyz

    var changed = transition.paramsChanged()
    // changed is { param2: 'xyz' }

    The value of param2 changed to xyz. The value of param1 stayed the same so its value is not present.

    Example 2

    From /stateA/abc/def to /stateB/123

    var changed = transition.paramsChanged()
    // changed is { param1: undefined, param2: undefined, param3: '123' }

    The value param3 is present because it is a new param. Both param1 and param2 are no longer present so their value is undefined.

    Example 3

    From /stateB/123 to /stateB/123/nest/456

    var changed = transition.paramsChanged()
    // changed is { param4: '456' }

    The value param4 is present because it is a new param. The value of param3 did not change, so its value is not present.

    Returns object

    :

    an immutable object with changed parameter keys/values.

    • [paramName: string]: any

  • Type parameters

    • T

    Returns T


  • redirect(targetState: TargetState): Transition
  • Creates a new transition that is a redirection of the current one.

  • Creates a new transition that is a redirection of the current one.

    This transition can be returned from a TransitionService hook to redirect a transition to a new state and/or set of parameters.

    Parameters

    Returns Transition

    :

    Returns a new Transition instance.


  • redirectedFrom(): Transition
  • Gets the transition from which this transition was redirected.

  • Gets the transition from which this transition was redirected.

    If the current transition is a redirect, this method returns the transition that was redirected.

    Example:

    let transitionA = $state.go('A').transition
    transitionA.onStart({}, () => $state.target('B'));
    $transitions.onSuccess({ to: 'B' }, (trans) => {
      trans.to().name === 'B'; // true
      trans.redirectedFrom() === transitionA; // true
    });

    Returns Transition

    :

    The previous Transition, or null if this Transition is not the result of a redirection


  • retained(): StateDeclaration[]
  • Gets the states being retained.

  • Gets the states being retained.

    Returns StateDeclaration[]

    :

    an array of states that are already entered from a previous Transition, that will not be exited during this Transition


  • run(): Promise<any>
  • Runs the transition

  • Runs the transition

    This method is generally called from the StateService.transitionTo

    Returns Promise<any>

    :

    a promise for a successful transition.


  • Gets the Target State

    A transition's TargetState encapsulates the to state, the params, and the options as a single object.

    Returns TargetState

    :

    the TargetState of this Transition


  • Returns the "to state"

    Returns the state that the transition is going to.

    Returns StateDeclaration

    :

    The state declaration object for the Transition's target state ("to state").


  • toString(): string
  • A string representation of the Transition

  • A string representation of the Transition

    Returns string

    :

    A string representation of the Transition


  • treeChanges(pathname: string): PathNode[]
  • Return the transition's tree changes

  • treeChanges(): TreeChanges
  • Return the transition's tree changes

    A transition goes from one state/parameters to another state/parameters. During a transition, states are entered and/or exited.

    This function returns various branches (paths) which represent the changes to the active state tree that are caused by the transition.

    Parameters

    • pathname string
      :

      The name of the tree changes path to get: ('to', 'from', 'entering', 'exiting', 'retained')

    Returns PathNode[]


  • Returns TreeChanges


  • valid(): boolean
  • Checks if the Transition is valid

  • Checks if the Transition is valid

    Returns boolean

    :

    true if the Transition is valid


  • views(pathname?: string, state?: StateObject): ViewConfig[]
  • Get the ViewConfigs associated with this Transition

  • Get the ViewConfigs associated with this Transition

    Each state can define one or more views (template/controller), which are encapsulated as ViewConfig objects. This method fetches the ViewConfigs for a given path in the Transition (e.g., "to" or "entering").

    Parameters

    • pathname: Default value  string = "entering"
      :

      the name of the path to fetch views for: ('to', 'from', 'entering', 'exiting', 'retained')

    • state: Optional  StateObject
      :

      If provided, only returns the ViewConfigs for a single state in the path

    Returns ViewConfig[]

    :

    a list of ViewConfig objects for the given path.


Generated using TypeDoc