Skip to content

Instantly share code, notes, and snippets.

@cognivator
Last active July 26, 2016 17:34
Show Gist options
  • Save cognivator/4dc6be86c603b9f3258161d415892b35 to your computer and use it in GitHub Desktop.
Save cognivator/4dc6be86c603b9f3258161d415892b35 to your computer and use it in GitHub Desktop.
showStates - console function to extract the configured states of ui-router and print them to the console as a JSON tree.

Usage

At the Javascript console, run showAllStates() to see a JSON tree of the currently configured ui-router states as returned by $state.get().

Installation

Copy showStates code below into your Javascript console (tested with Chrome Devtools), and execute it to create the showStates function. Optionally, create similar functions in the console with the other scripts in this gist.

showAllStates = function () {
	var $injector = angular.element('html').injector(),
	$state = $injector.get('$state'),
	states = $state.get(),
	sObj = _(states).sortBy('name').reduce(reducer, {});

	return JSON.stringify(sObj, null, 2);

	function reducer(acc, val, key, col) {
		return _.set(acc, val.name, val);
	}
}

Requirements

Assumes Angular and ui-router have already been bootstrapped. Assumes lodash is loaded.

cState = function () {
var $injector = angular.element('html').injector(),
$state = $injector.get('$state'),
states = [$state.current],
sObj = _(states).sortBy('name').reduce(reducer, {});
return JSON.stringify(sObj, null, 2);
function reducer(acc, val, key, col) {
return _.set(acc, val.name, val);
}
};
stateService = function () {
var $injector = angular.element('html').injector(),
$state = $injector.get('$state');
return $state;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment