$state
(service in module ui.router.state
)
$state
service is responsible for representing states as well as transitioning
between them. It also provides interfaces to ask for current state or even states
you're coming from.
Returns the state configuration object for any state by passing the name as a string. Without any arguments it'll return a array of all configured state objects.
stateOrName – {string|object} –
The name of the state for which you'd like to get the original state configuration object for.
{object}
– State configuration object or array of all objects.
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 current state.
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 stateto – {string} –
Absolute State Name or Relative State Path.
params – {object} –
A map of the parameters that will be sent to the state, will populate $stateParams.
options – {object} –
If Object is passed, object is an options hash.
var app = angular.module('app', ['ui.router.state']); app.controller('ctrl', function ($scope, $state) { $scope.changeState = function () { $state.go('contact.detail'); }; });
A url generation method that returns the compiled url for the given state populated with the given params.
stateOrName – {string|object} –
The state name or state object you'd like to generate a url from.
params – {object} –
An object of parameter values to fill the state's required parameters.
{string}
– url
expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
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.
stateOrName – {string} –
A partial name to be searched for within the current state name.
params – {object} –
A param object, e.g. {sectionId: section.id}
,
that you'd like to test against the current active state.
{boolean}
– True or false
$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
Similar to $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.
stateName – {string|object} –
The state name or state object you'd like to check.
params – {object} –
A param object, e.g. {sectionId: section.id}
, that you'd like
to test against the current active state.
{boolean}
– Returns true or false whether its the state or not.
$state.is('contact.details.item'); // returns true $state.is(contactDetailItemStateObject); // returns true // everything else would return false
Reloads the current state by re-transitioning to it.
var app angular.module('app', ['ui.router.state']); app.controller('ctrl', function ($state) { $state.reload(); });
Low-level method for transitioning to a new state. $state.go
uses transitionTo
internally. $state.go
is recommended in most situations.
to – {string} –
Absolute State Name or Relative State Path.
params – {object} –
A map of the parameters that will be sent to the state, will populate $stateParams.
options – {object} –
If Object is passed, object is an options hash.
var app = angular.module('app', ['ui.router.state']); app.controller('ctrl', function ($scope, $state) { $scope.changeState = function () { $state.transitionTo('contact.detail'); }; });
A param object, e.g. {sectionId: section.id)}, that you'd like to test against the current active state.
A reference to the state's config object. However you passed it in. Useful for accessing custom data.
Currently pending transition. A promise that'll resolve or reject.