Skip to content

Commit 929eacc

Browse files
committed
docs(state): finishes documentation for state
1 parent 58936a5 commit 929eacc

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

src/state.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,15 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
479479
*
480480
* @description
481481
* Reloads the current state by re-transitioning to it.
482+
*
483+
* @example
484+
* <pre>
485+
* var app angular.module('app', ['ui.router.state']);
486+
*
487+
* app.controller('ctrl', function ($state) {
488+
* $state.reload();
489+
* });
490+
* </pre>
482491
*/
483492
$state.reload = function reload() {
484493
$state.transitionTo($state.current, $stateParams, { reload: true, inherit: false, notify: false });
@@ -497,6 +506,24 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
497506
* only the parameters you'd like to update (while letting unspecified parameters
498507
* inherit from the current state.
499508
*
509+
* Some examples:
510+
*
511+
* - `$state.go('contact.detail')` - will go to the `contact.detail` state
512+
* - `$state.go('^')` - will go to a parent state
513+
* - `$state.go('^.sibling')` - will go to a sibling state
514+
* - `$state.go('.child.grandchild')` - will go to grandchild state
515+
*
516+
* @example
517+
* <pre>
518+
* var app = angular.module('app', ['ui.router.state']);
519+
*
520+
* app.controller('ctrl', function ($scope, $state) {
521+
* $scope.changeState = function () {
522+
* $state.go('contact.detail');
523+
* };
524+
* });
525+
* </pre>
526+
*
500527
* @param {string} to Absolute State Name or Relative State Path.
501528
* @param {object} params A map of the parameters that will be sent to the state,
502529
* will populate $stateParams.
@@ -506,6 +533,31 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
506533
return this.transitionTo(to, params, extend({ inherit: true, relative: $state.$current }, options));
507534
};
508535

536+
/**
537+
* @ngdoc function
538+
* @name ui.router.state.$state#transitionTo
539+
* @methodOf ui.router.state.$state
540+
*
541+
* @description
542+
* Low-level method for transitioning to a new state. {@link ui.router.state.$state#methods_go $state.go}
543+
* uses `transitionTo` internally. `$state.go` is recommended in most situations.
544+
*
545+
* @example
546+
* <pre>
547+
* var app = angular.module('app', ['ui.router.state']);
548+
*
549+
* app.controller('ctrl', function ($scope, $state) {
550+
* $scope.changeState = function () {
551+
* $state.transitionTo('contact.detail');
552+
* };
553+
* });
554+
* </pre>
555+
*
556+
* @param {string} to Absolute State Name or Relative State Path.
557+
* @param {object} params A map of the parameters that will be sent to the state,
558+
* will populate $stateParams.
559+
* @param {object} options If Object is passed, object is an options hash.
560+
*/
509561
$state.transitionTo = function transitionTo(to, toParams, options) {
510562
toParams = toParams || {};
511563
options = extend({
@@ -669,6 +721,30 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
669721
return transition;
670722
};
671723

724+
/**
725+
* @ngdoc function
726+
* @name ui.router.state.$state#is
727+
* @methodOf ui.router.state.$state
728+
*
729+
* @description
730+
* Similar to {@link ui.router.state.$state#methods_includes $state.includes},
731+
* but only checks for the full state name. If params is supplied then it will be
732+
* tested for strict equality against the current active params object, so all params
733+
* must match with none missing and no extras.
734+
*
735+
* @example
736+
* <pre>
737+
* $state.is('contact.details.item'); // returns true
738+
* $state.is(contactDetailItemStateObject); // returns true
739+
*
740+
* // everything else would return false
741+
* </pre>
742+
*
743+
* @param {string|object} stateName The state name or state object you'd like to check.
744+
* @param {object} params A param object, e.g. `{sectionId: section.id}`, that you'd like
745+
* to test against the current active state.
746+
* @returns {boolean} Returns true or false whether its the state or not.
747+
*/
672748
$state.is = function is(stateOrName, params) {
673749
var state = findState(stateOrName);
674750

@@ -683,6 +759,30 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
683759
return isDefined(params) ? angular.equals($stateParams, params) : true;
684760
};
685761

762+
/**
763+
* @ngdoc function
764+
* @name ui.router.state.$state#includes
765+
* @methodOf ui.router.state.$state
766+
*
767+
* @description
768+
* A method to determine if the current active state is equal to or is the child of the
769+
* state stateName. If any params are passed then they will be tested for a match as well.
770+
* Not all the parameters need to be passed, just the ones you'd like to test for equality.
771+
*
772+
* @example
773+
* <pre>
774+
* $state.includes("contacts"); // returns true
775+
* $state.includes("contacts.details"); // returns true
776+
* $state.includes("contacts.details.item"); // returns true
777+
* $state.includes("contacts.list"); // returns false
778+
* $state.includes("about"); // returns false
779+
* </pre>
780+
*
781+
* @param {string} stateOrName A partial name to be searched for within the current state name.
782+
* @param {object} params A param object, e.g. `{sectionId: section.id}`,
783+
* that you'd like to test against the current active state.
784+
* @returns {boolean} True or false
785+
*/
686786
$state.includes = function includes(stateOrName, params) {
687787
var state = findState(stateOrName);
688788
if (!isDefined(state)) {
@@ -702,6 +802,23 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
702802
return validParams;
703803
};
704804

805+
/**
806+
* @ngdoc function
807+
* @name ui.router.state.$state#href
808+
* @methodOf ui.router.state.$state
809+
*
810+
* @description
811+
* A url generation method that returns the compiled url for the given state populated with the given params.
812+
*
813+
* @example
814+
* <pre>
815+
* expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
816+
* </pre>
817+
*
818+
* @param {string|object} stateOrName The state name or state object you'd like to generate a url from.
819+
* @param {object} params An object of parameter values to fill the state's required parameters.
820+
* @returns {string} url
821+
*/
705822
$state.href = function href(stateOrName, params, options) {
706823
options = extend({ lossy: true, inherit: false, absolute: false, relative: $state.$current }, options || {});
707824
var state = findState(stateOrName, options.relative);
@@ -723,6 +840,20 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
723840
return url;
724841
};
725842

843+
/**
844+
* @ngdoc function
845+
* @name ui.router.state.$state#get
846+
* @methodOf ui.router.state.$state
847+
*
848+
* @description
849+
* Returns the state configuration object for any state by passing the name
850+
* as a string. Without any arguments it'll return a array of all configured
851+
* state objects.
852+
*
853+
* @param {string|object} stateOrName The name of the state for which you'd like
854+
* to get the original state configuration object for.
855+
* @returns {object} State configuration object or array of all objects.
856+
*/
726857
$state.get = function (stateOrName, context) {
727858
if (!isDefined(stateOrName)) {
728859
var list = [];

0 commit comments

Comments
 (0)