3 * @name ui.router.state.$stateProvider
5 * @requires ui.router.router.$urlRouterProvider
6 * @requires ui.router.util.$urlMatcherFactoryProvider
9 * The new `$stateProvider` works similar to Angular's v1 router, but it focuses purely
12 * A state corresponds to a "place" in the application in terms of the overall UI and
13 * navigation. A state describes (via the controller / template / view properties) what
14 * the UI looks like and does at that place.
16 * States often have things in common, and the primary way of factoring out these
17 * commonalities in this model is via the state hierarchy, i.e. parent/child states aka
20 * The `$stateProvider` provides interfaces to declare these states for your app.
22 $StateProvider.$inject = ['$urlRouterProvider', '$urlMatcherFactoryProvider'];
23 function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
25 var root, states = {}, $state, queue = {}, abstractKey = 'abstract';
27 // Builds state properties from definition passed to registerState()
30 // Derive parent state from a hierarchical name only if 'parent' is not explicitly defined.
31 // state.children = [];
32 // if (parent) parent.children.push(state);
33 parent: function(state) {
34 if (isDefined(state.parent) && state.parent) return findState(state.parent);
35 // regex matches any valid composite state name
36 // would match "contact.list" but not "contacts"
37 var compositeName = /^(.+)\.[^.]+$/.exec(state.name);
38 return compositeName ? findState(compositeName[1]) : root;
41 // inherit 'data' from parent and override by own values (if any)
42 data: function(state) {
43 if (state.parent && state.parent.data) {
44 state.data = state.self.data = inherit(state.parent.data, state.data);
49 // Build a URLMatcher if necessary, either via a relative or absolute URL
50 url: function(state) {
51 var url = state.url, config = { params: state.params || {} };
54 if (url.charAt(0) == '^') return $urlMatcherFactory.compile(url.substring(1), config);
55 return (state.parent.navigable || root).url.concat(url, config);
58 if (!url || $urlMatcherFactory.isMatcher(url)) return url;
59 throw new Error("Invalid url '" + url + "' in state '" + state + "'");
62 // Keep track of the closest ancestor state that has a URL (i.e. is navigable)
63 navigable: function(state) {
64 return state.url ? state : (state.parent ? state.parent.navigable : null);
67 // Own parameters for this state. state.url.params is already built at this point. Create and add non-url params
68 ownParams: function(state) {
69 var params = state.url && state.url.params || new $$UMFP.ParamSet();
70 forEach(state.params || {}, function(config, id) {
71 if (!params[id]) params[id] = new $$UMFP.Param(id, null, config, "config");
76 // Derive parameters for this state and ensure they're a super-set of parent's parameters
77 params: function(state) {
78 return state.parent && state.parent.params ? extend(state.parent.params.$$new(), state.ownParams) : new $$UMFP.ParamSet();
81 // If there is no explicit multi-view configuration, make one up so we don't have
82 // to handle both cases in the view directive later. Note that having an explicit
83 // 'views' property will mean the default unnamed view properties are ignored. This
84 // is also a good time to resolve view names to absolute names, so everything is a
85 // straight lookup at link time.
86 views: function(state) {
89 forEach(isDefined(state.views) ? state.views : { '': state }, function (view, name) {
90 if (name.indexOf('@') < 0) name += '@' + state.parent.name;
96 // Keep a full path from the root down to this state as this is needed for state activation.
97 path: function(state) {
98 return state.parent ? state.parent.path.concat(state) : []; // exclude root from path
101 // Speed up $state.contains() as it's used a lot
102 includes: function(state) {
103 var includes = state.parent ? extend({}, state.parent.includes) : {};
104 includes[state.name] = true;
111 function isRelative(stateName) {
112 return stateName.indexOf(".") === 0 || stateName.indexOf("^") === 0;
115 function findState(stateOrName, base) {
116 if (!stateOrName) return undefined;
118 var isStr = isString(stateOrName),
119 name = isStr ? stateOrName : stateOrName.name,
120 path = isRelative(name);
123 if (!base) throw new Error("No reference point given for path '" + name + "'");
124 base = findState(base);
126 var rel = name.split("."), i = 0, pathLength = rel.length, current = base;
128 for (; i < pathLength; i++) {
129 if (rel[i] === "" && i === 0) {
133 if (rel[i] === "^") {
134 if (!current.parent) throw new Error("Path '" + name + "' not valid for state '" + base.name + "'");
135 current = current.parent;
140 rel = rel.slice(i).join(".");
141 name = current.name + (current.name && rel ? "." : "") + rel;
143 var state = states[name];
145 if (state && (isStr || (!isStr && (state === stateOrName || state.self === stateOrName)))) {
151 function queueState(parentName, state) {
152 if (!queue[parentName]) {
153 queue[parentName] = [];
155 queue[parentName].push(state);
158 function flushQueuedChildren(parentName) {
159 var queued = queue[parentName] || [];
160 while(queued.length) {
161 registerState(queued.shift());
165 function registerState(state) {
166 // Wrap a new object around the state so we can store our private details easily.
167 state = inherit(state, {
169 resolve: state.resolve || {},
170 toString: function() { return this.name; }
173 var name = state.name;
174 if (!isString(name) || name.indexOf('@') >= 0) throw new Error("State must have a valid name");
175 if (states.hasOwnProperty(name)) throw new Error("State '" + name + "' is already defined");
178 var parentName = (name.indexOf('.') !== -1) ? name.substring(0, name.lastIndexOf('.'))
179 : (isString(state.parent)) ? state.parent
180 : (isObject(state.parent) && isString(state.parent.name)) ? state.parent.name
183 // If parent is not registered yet, add state to queue and register later
184 if (parentName && !states[parentName]) {
185 return queueState(parentName, state.self);
188 for (var key in stateBuilder) {
189 if (isFunction(stateBuilder[key])) state[key] = stateBuilder[key](state, stateBuilder.$delegates[key]);
191 states[name] = state;
193 // Register the state in the global state list and with $urlRouter if necessary.
194 if (!state[abstractKey] && state.url) {
195 $urlRouterProvider.when(state.url, ['$match', '$stateParams', function ($match, $stateParams) {
196 if ($state.$current.navigable != state || !equalForKeys($match, $stateParams)) {
197 $state.transitionTo(state, $match, { inherit: true, location: false });
202 // Register any queued children
203 flushQueuedChildren(name);
208 // Checks text to see if it looks like a glob.
209 function isGlob (text) {
210 return text.indexOf('*') > -1;
213 // Returns true if glob matches current $state name.
214 function doesStateMatchGlob (glob) {
215 var globSegments = glob.split('.'),
216 segments = $state.$current.name.split('.');
219 for (var i = 0, l = globSegments.length; i < l; i++) {
220 if (globSegments[i] === '*') {
225 //match greedy starts
226 if (globSegments[0] === '**') {
227 segments = segments.slice(indexOf(segments, globSegments[1]));
228 segments.unshift('**');
231 if (globSegments[globSegments.length - 1] === '**') {
232 segments.splice(indexOf(segments, globSegments[globSegments.length - 2]) + 1, Number.MAX_VALUE);
236 if (globSegments.length != segments.length) {
240 return segments.join('') === globSegments.join('');
244 // Implicit root state that is always active
245 root = registerState({
251 root.navigable = null;
256 * @name ui.router.state.$stateProvider#decorator
257 * @methodOf ui.router.state.$stateProvider
260 * Allows you to extend (carefully) or override (at your own peril) the
261 * `stateBuilder` object used internally by `$stateProvider`. This can be used
262 * to add custom functionality to ui-router, for example inferring templateUrl
263 * based on the state name.
265 * When passing only a name, it returns the current (original or decorated) builder
266 * function that matches `name`.
268 * The builder functions that can be decorated are listed below. Though not all
269 * necessarily have a good use case for decoration, that is up to you to decide.
271 * In addition, users can attach custom decorators, which will generate new
272 * properties within the state's internal definition. There is currently no clear
273 * use-case for this beyond accessing internal states (i.e. $state.$current),
274 * however, expect this to become increasingly relevant as we introduce additional
275 * meta-programming features.
277 * **Warning**: Decorators should not be interdependent because the order of
278 * execution of the builder functions in non-deterministic. Builder functions
279 * should only be dependent on the state definition object and super function.
282 * Existing builder functions and current return values:
284 * - **parent** `{object}` - returns the parent state object.
285 * - **data** `{object}` - returns state data, including any inherited data that is not
286 * overridden by own values (if any).
287 * - **url** `{object}` - returns a {@link ui.router.util.type:UrlMatcher UrlMatcher}
289 * - **navigable** `{object}` - returns closest ancestor state that has a URL (aka is
291 * - **params** `{object}` - returns an array of state params that are ensured to
292 * be a super-set of parent's params.
293 * - **views** `{object}` - returns a views object where each key is an absolute view
294 * name (i.e. "viewName@stateName") and each value is the config object
295 * (template, controller) for the view. Even when you don't use the views object
296 * explicitly on a state config, one is still created for you internally.
297 * So by decorating this builder function you have access to decorating template
298 * and controller properties.
299 * - **ownParams** `{object}` - returns an array of params that belong to the state,
300 * not including any params defined by ancestor states.
301 * - **path** `{string}` - returns the full path from the root down to this state.
302 * Needed for state activation.
303 * - **includes** `{object}` - returns an object that includes every state that
304 * would pass a `$state.includes()` test.
308 * // Override the internal 'views' builder with a function that takes the state
309 * // definition, and a reference to the internal function being overridden:
310 * $stateProvider.decorator('views', function (state, parent) {
312 * views = parent(state);
314 * angular.forEach(views, function (config, name) {
315 * var autoName = (state.name + '.' + name).replace('.', '/');
316 * config.templateUrl = config.templateUrl || '/partials/' + autoName + '.html';
317 * result[name] = config;
322 * $stateProvider.state('home', {
324 * 'contact.list': { controller: 'ListController' },
325 * 'contact.item': { controller: 'ItemController' }
332 * // Auto-populates list and item views with /partials/home/contact/list.html,
333 * // and /partials/home/contact/item.html, respectively.
336 * @param {string} name The name of the builder function to decorate.
337 * @param {object} func A function that is responsible for decorating the original
338 * builder function. The function receives two parameters:
340 * - `{object}` - state - The state config object.
341 * - `{object}` - super - The original builder function.
343 * @return {object} $stateProvider - $stateProvider instance
345 this.decorator = decorator;
346 function decorator(name, func) {
347 /*jshint validthis: true */
348 if (isString(name) && !isDefined(func)) {
349 return stateBuilder[name];
351 if (!isFunction(func) || !isString(name)) {
354 if (stateBuilder[name] && !stateBuilder.$delegates[name]) {
355 stateBuilder.$delegates[name] = stateBuilder[name];
357 stateBuilder[name] = func;
363 * @name ui.router.state.$stateProvider#state
364 * @methodOf ui.router.state.$stateProvider
367 * Registers a state configuration under a given state name. The stateConfig object
368 * has the following acceptable properties.
370 * @param {string} name A unique state name, e.g. "home", "about", "contacts".
371 * To create a parent/child state use a dot, e.g. "about.sales", "home.newest".
372 * @param {object} stateConfig State configuration object.
373 * @param {string|function=} stateConfig.template
374 * <a id='template'></a>
375 * html template as a string or a function that returns
376 * an html template as a string which should be used by the uiView directives. This property
377 * takes precedence over templateUrl.
379 * If `template` is a function, it will be called with the following parameters:
381 * - {array.<object>} - state parameters extracted from the current $location.path() by
382 * applying the current state
385 * "<h1>inline template definition</h1>" +
386 * "<div ui-view></div>"</pre>
387 * <pre>template: function(params) {
388 * return "<h1>generated template</h1>"; }</pre>
391 * @param {string|function=} stateConfig.templateUrl
392 * <a id='templateUrl'></a>
394 * path or function that returns a path to an html
395 * template that should be used by uiView.
397 * If `templateUrl` is a function, it will be called with the following parameters:
399 * - {array.<object>} - state parameters extracted from the current $location.path() by
400 * applying the current state
402 * <pre>templateUrl: "home.html"</pre>
403 * <pre>templateUrl: function(params) {
404 * return myTemplates[params.pageId]; }</pre>
406 * @param {function=} stateConfig.templateProvider
407 * <a id='templateProvider'></a>
408 * Provider function that returns HTML content string.
409 * <pre> templateProvider:
410 * function(MyTemplateService, params) {
411 * return MyTemplateService.getTemplate(params.pageId);
414 * @param {string|function=} stateConfig.controller
415 * <a id='controller'></a>
417 * Controller fn that should be associated with newly
418 * related scope or the name of a registered controller if passed as a string.
419 * Optionally, the ControllerAs may be declared here.
420 * <pre>controller: "MyRegisteredController"</pre>
422 * "MyRegisteredController as fooCtrl"}</pre>
423 * <pre>controller: function($scope, MyService) {
424 * $scope.data = MyService.getData(); }</pre>
426 * @param {function=} stateConfig.controllerProvider
427 * <a id='controllerProvider'></a>
429 * Injectable provider function that returns the actual controller or string.
430 * <pre>controllerProvider:
431 * function(MyResolveData) {
432 * if (MyResolveData.foo)
434 * else if (MyResolveData.bar)
436 * else return function($scope) {
437 * $scope.baz = "Qux";
441 * @param {string=} stateConfig.controllerAs
442 * <a id='controllerAs'></a>
444 * A controller alias name. If present the controller will be
445 * published to scope under the controllerAs name.
446 * <pre>controllerAs: "myCtrl"</pre>
448 * @param {string|object=} stateConfig.parent
449 * <a id='parent'></a>
450 * Optionally specifies the parent state of this state.
452 * <pre>parent: 'parentState'</pre>
453 * <pre>parent: parentState // JS variable</pre>
455 * @param {object=} stateConfig.resolve
456 * <a id='resolve'></a>
458 * An optional map<string, function> of dependencies which
459 * should be injected into the controller. If any of these dependencies are promises,
460 * the router will wait for them all to be resolved before the controller is instantiated.
461 * If all the promises are resolved successfully, the $stateChangeSuccess event is fired
462 * and the values of the resolved promises are injected into any controllers that reference them.
463 * If any of the promises are rejected the $stateChangeError event is fired.
467 * - key - {string}: name of dependency to be injected into controller
468 * - factory - {string|function}: If string then it is alias for service. Otherwise if function,
469 * it is injected and return value it treated as dependency. If result is a promise, it is
470 * resolved before its value is injected into controller.
474 * function($http, $stateParams) {
475 * return $http.get("/api/foos/"+stateParams.fooID);
479 * @param {string=} stateConfig.url
482 * A url fragment with optional parameters. When a state is navigated or
483 * transitioned to, the `$stateParams` service will be populated with any
484 * parameters that were passed.
486 * (See {@link ui.router.util.type:UrlMatcher UrlMatcher} `UrlMatcher`} for
487 * more details on acceptable patterns )
491 * url: "/users/:userid"
492 * url: "/books/{bookid:[a-zA-Z_-]}"
493 * url: "/books/{categoryid:int}"
494 * url: "/books/{publishername:string}/{categoryid:int}"
495 * url: "/messages?before&after"
496 * url: "/messages?{before:date}&{after:date}"
497 * url: "/messages/:mailboxid?{before:date}&{after:date}"
500 * @param {object=} stateConfig.views
502 * an optional map<string, object> which defined multiple views, or targets views
503 * manually/explicitly.
507 * Targets three named `ui-view`s in the parent state's template
510 * controller: "headerCtrl",
511 * templateUrl: "header.html"
513 * controller: "bodyCtrl",
514 * templateUrl: "body.html"
516 * controller: "footCtrl",
517 * templateUrl: "footer.html"
521 * Targets named `ui-view="header"` from grandparent state 'top''s template, and named `ui-view="body" from parent state's template.
524 * controller: "msgHeaderCtrl",
525 * templateUrl: "msgHeader.html"
527 * controller: "messagesCtrl",
528 * templateUrl: "messages.html"
532 * @param {boolean=} [stateConfig.abstract=false]
533 * <a id='abstract'></a>
534 * An abstract state will never be directly activated,
535 * but can provide inherited properties to its common children states.
536 * <pre>abstract: true</pre>
538 * @param {function=} stateConfig.onEnter
539 * <a id='onEnter'></a>
541 * Callback function for when a state is entered. Good way
542 * to trigger an action or dispatch an event, such as opening a dialog.
543 * If minifying your scripts, make sure to explicitly annotate this function,
544 * because it won't be automatically annotated by your build tools.
546 * <pre>onEnter: function(MyService, $stateParams) {
547 * MyService.foo($stateParams.myParam);
550 * @param {function=} stateConfig.onExit
551 * <a id='onExit'></a>
553 * Callback function for when a state is exited. Good way to
554 * trigger an action or dispatch an event, such as opening a dialog.
555 * If minifying your scripts, make sure to explicitly annotate this function,
556 * because it won't be automatically annotated by your build tools.
558 * <pre>onExit: function(MyService, $stateParams) {
559 * MyService.cleanup($stateParams.myParam);
562 * @param {boolean=} [stateConfig.reloadOnSearch=true]
563 * <a id='reloadOnSearch'></a>
565 * If `false`, will not retrigger the same state
566 * just because a search/query parameter has changed (via $location.search() or $location.hash()).
567 * Useful for when you'd like to modify $location.search() without triggering a reload.
568 * <pre>reloadOnSearch: false</pre>
570 * @param {object=} stateConfig.data
573 * Arbitrary data object, useful for custom configuration. The parent state's `data` is
574 * prototypally inherited. In other words, adding a data property to a state adds it to
575 * the entire subtree via prototypal inheritance.
578 * requiredRole: 'foo'
581 * @param {object=} stateConfig.params
582 * <a id='params'></a>
584 * A map which optionally configures parameters declared in the `url`, or
585 * defines additional non-url parameters. For each parameter being
586 * configured, add a configuration object keyed to the name of the parameter.
588 * Each parameter configuration object may contain the following properties:
590 * - ** value ** - {object|function=}: specifies the default value for this
591 * parameter. This implicitly sets this parameter as optional.
593 * When UI-Router routes to a state and no value is
594 * specified for this parameter in the URL or transition, the
595 * default value will be used instead. If `value` is a function,
596 * it will be injected and invoked, and the return value used.
598 * *Note*: `undefined` is treated as "no default value" while `null`
599 * is treated as "the default value is `null`".
601 * *Shorthand*: If you only need to configure the default value of the
602 * parameter, you may use a shorthand syntax. In the **`params`**
603 * map, instead mapping the param name to a full parameter configuration
604 * object, simply set map it to the default parameter value, e.g.:
606 * <pre>// define a parameter's default value
608 * param1: { value: "defaultValue" }
610 * // shorthand default values
612 * param1: "defaultValue",
613 * param2: "param2Default"
616 * - ** array ** - {boolean=}: *(default: false)* If true, the param value will be
617 * treated as an array of values. If you specified a Type, the value will be
618 * treated as an array of the specified Type. Note: query parameter values
619 * default to a special `"auto"` mode.
621 * For query parameters in `"auto"` mode, if multiple values for a single parameter
622 * are present in the URL (e.g.: `/foo?bar=1&bar=2&bar=3`) then the values
623 * are mapped to an array (e.g.: `{ foo: [ '1', '2', '3' ] }`). However, if
624 * only one value is present (e.g.: `/foo?bar=1`) then the value is treated as single
625 * value (e.g.: `{ foo: '1' }`).
628 * param1: { array: true }
631 * - ** squash ** - {bool|string=}: `squash` configures how a default parameter value is represented in the URL when
632 * the current parameter value is the same as the default value. If `squash` is not set, it uses the
633 * configured default squash policy.
634 * (See {@link ui.router.util.$urlMatcherFactory#methods_defaultSquashPolicy `defaultSquashPolicy()`})
636 * There are three squash settings:
638 * - false: The parameter's default value is not squashed. It is encoded and included in the URL
639 * - true: The parameter's default value is omitted from the URL. If the parameter is preceeded and followed
640 * by slashes in the state's `url` declaration, then one of those slashes are omitted.
641 * This can allow for cleaner looking URLs.
642 * - `"<arbitrary string>"`: The parameter's default value is replaced with an arbitrary placeholder of your choice.
646 * value: "defaultId",
649 * // squash "defaultValue" to "~"
652 * value: "defaultValue",
660 * // Some state name examples
662 * // stateName can be a single top-level name (must be unique).
663 * $stateProvider.state("home", {});
665 * // Or it can be a nested state name. This state is a child of the
666 * // above "home" state.
667 * $stateProvider.state("home.newest", {});
669 * // Nest states as deeply as needed.
670 * $stateProvider.state("home.newest.abc.xyz.inception", {});
672 * // state() returns $stateProvider, so you can chain state declarations.
675 * .state("about", {})
676 * .state("contacts", {});
681 function state(name, definition) {
682 /*jshint validthis: true */
683 if (isObject(name)) definition = name;
684 else definition.name = name;
685 registerState(definition);
691 * @name ui.router.state.$state
693 * @requires $rootScope
695 * @requires ui.router.state.$view
696 * @requires $injector
697 * @requires ui.router.util.$resolve
698 * @requires ui.router.state.$stateParams
699 * @requires ui.router.router.$urlRouter
701 * @property {object} params A param object, e.g. {sectionId: section.id)}, that
702 * you'd like to test against the current active state.
703 * @property {object} current A reference to the state's config object. However
704 * you passed it in. Useful for accessing custom data.
705 * @property {object} transition Currently pending transition. A promise that'll
709 * `$state` service is responsible for representing states as well as transitioning
710 * between them. It also provides interfaces to ask for current state or even states
711 * you're coming from.
714 $get.$inject = ['$rootScope', '$q', '$view', '$injector', '$resolve', '$stateParams', '$urlRouter', '$location', '$urlMatcherFactory'];
715 function $get( $rootScope, $q, $view, $injector, $resolve, $stateParams, $urlRouter, $location, $urlMatcherFactory) {
717 var TransitionSuperseded = $q.reject(new Error('transition superseded'));
718 var TransitionPrevented = $q.reject(new Error('transition prevented'));
719 var TransitionAborted = $q.reject(new Error('transition aborted'));
720 var TransitionFailed = $q.reject(new Error('transition failed'));
722 // Handles the case where a state which is the target of a transition is not found, and the user
723 // can optionally retry or defer the transition
724 function handleRedirect(redirect, state, params, options) {
727 * @name ui.router.state.$state#$stateNotFound
728 * @eventOf ui.router.state.$state
729 * @eventType broadcast on root scope
731 * Fired when a requested state **cannot be found** using the provided state name during transition.
732 * The event is broadcast allowing any handlers a single chance to deal with the error (usually by
733 * lazy-loading the unfound state). A special `unfoundState` object is passed to the listener handler,
734 * you can see its three properties in the example. You can use `event.preventDefault()` to abort the
735 * transition and the promise returned from `go` will be rejected with a `'transition aborted'` value.
737 * @param {Object} event Event object.
738 * @param {Object} unfoundState Unfound State information. Contains: `to, toParams, options` properties.
739 * @param {State} fromState Current state object.
740 * @param {Object} fromParams Current state params.
745 * // somewhere, assume lazy.state has not been defined
746 * $state.go("lazy.state", {a:1, b:2}, {inherit:false});
749 * $scope.$on('$stateNotFound',
750 * function(event, unfoundState, fromState, fromParams){
751 * console.log(unfoundState.to); // "lazy.state"
752 * console.log(unfoundState.toParams); // {a:1, b:2}
753 * console.log(unfoundState.options); // {inherit:false} + default options
757 var evt = $rootScope.$broadcast('$stateNotFound', redirect, state, params);
759 if (evt.defaultPrevented) {
761 return TransitionAborted;
768 // Allow the handler to return a promise to defer state lookup retry
769 if (options.$retry) {
771 return TransitionFailed;
773 var retryTransition = $state.transition = $q.when(evt.retry);
775 retryTransition.then(function() {
776 if (retryTransition !== $state.transition) return TransitionSuperseded;
777 redirect.options.$retry = true;
778 return $state.transitionTo(redirect.to, redirect.toParams, redirect.options);
780 return TransitionAborted;
784 return retryTransition;
787 root.locals = { resolve: null, globals: { $stateParams: {} } };
798 * @name ui.router.state.$state#reload
799 * @methodOf ui.router.state.$state
802 * A method that force reloads the current state. All resolves are re-resolved,
803 * controllers reinstantiated, and events re-fired.
807 * var app angular.module('app', ['ui.router']);
809 * app.controller('ctrl', function ($scope, $state) {
810 * $scope.reload = function(){
816 * `reload()` is just an alias for:
818 * $state.transitionTo($state.current, $stateParams, {
819 * reload: true, inherit: false, notify: true
823 * @param {string=|object=} state - A state name or a state object, which is the root of the resolves to be re-resolved.
826 * //assuming app application consists of 3 states: 'contacts', 'contacts.detail', 'contacts.detail.item'
827 * //and current state is 'contacts.detail.item'
828 * var app angular.module('app', ['ui.router']);
830 * app.controller('ctrl', function ($scope, $state) {
831 * $scope.reload = function(){
832 * //will reload 'contact.detail' and 'contact.detail.item' states
833 * $state.reload('contact.detail');
838 * `reload()` is just an alias for:
840 * $state.transitionTo($state.current, $stateParams, {
841 * reload: true, inherit: false, notify: true
845 * @returns {promise} A promise representing the state of the new transition. See
846 * {@link ui.router.state.$state#methods_go $state.go}.
848 $state.reload = function reload(state) {
849 return $state.transitionTo($state.current, $stateParams, { reload: state || true, inherit: false, notify: true});
854 * @name ui.router.state.$state#go
855 * @methodOf ui.router.state.$state
858 * Convenience method for transitioning to a new state. `$state.go` calls
859 * `$state.transitionTo` internally but automatically sets options to
860 * `{ location: true, inherit: true, relative: $state.$current, notify: true }`.
861 * This allows you to easily use an absolute or relative to path and specify
862 * only the parameters you'd like to update (while letting unspecified parameters
863 * inherit from the currently active ancestor states).
867 * var app = angular.module('app', ['ui.router']);
869 * app.controller('ctrl', function ($scope, $state) {
870 * $scope.changeState = function () {
871 * $state.go('contact.detail');
875 * <img src='../ngdoc_assets/StateGoExamples.png'/>
877 * @param {string} to Absolute state name or relative state path. Some examples:
879 * - `$state.go('contact.detail')` - will go to the `contact.detail` state
880 * - `$state.go('^')` - will go to a parent state
881 * - `$state.go('^.sibling')` - will go to a sibling state
882 * - `$state.go('.child.grandchild')` - will go to grandchild state
884 * @param {object=} params A map of the parameters that will be sent to the state,
885 * will populate $stateParams. Any parameters that are not specified will be inherited from currently
886 * defined parameters. Only parameters specified in the state definition can be overridden, new
887 * parameters will be ignored. This allows, for example, going to a sibling state that shares parameters
888 * specified in a parent state. Parameter inheritance only works between common ancestor states, I.e.
889 * transitioning to a sibling will get you the parameters for all parents, transitioning to a child
890 * will get you all current parameters, etc.
891 * @param {object=} options Options object. The options are:
893 * - **`location`** - {boolean=true|string=} - If `true` will update the url in the location bar, if `false`
894 * will not. If string, must be `"replace"`, which will update url and also replace last history record.
895 * - **`inherit`** - {boolean=true}, If `true` will inherit url parameters from current url.
896 * - **`relative`** - {object=$state.$current}, When transitioning with relative path (e.g '^'),
897 * defines which state to be relative from.
898 * - **`notify`** - {boolean=true}, If `true` will broadcast $stateChangeStart and $stateChangeSuccess events.
899 * - **`reload`** (v0.2.5) - {boolean=false|string|object}, If `true` will force transition even if no state or params
900 * have changed. It will reload the resolves and views of the current state and parent states.
901 * If `reload` is a string (or state object), the state object is fetched (by name, or object reference); and \
902 * the transition reloads the resolves and views for that matched state, and all its children states.
904 * @returns {promise} A promise representing the state of the new transition.
906 * Possible success values:
910 * <br/>Possible rejection values:
912 * - 'transition superseded' - when a newer transition has been started after this one
913 * - 'transition prevented' - when `event.preventDefault()` has been called in a `$stateChangeStart` listener
914 * - 'transition aborted' - when `event.preventDefault()` has been called in a `$stateNotFound` listener or
915 * when a `$stateNotFound` `event.retry` promise errors.
916 * - 'transition failed' - when a state has been unsuccessfully found after 2 tries.
917 * - *resolve error* - when an error has occurred with a `resolve`
920 $state.go = function go(to, params, options) {
921 return $state.transitionTo(to, params, extend({ inherit: true, relative: $state.$current }, options));
926 * @name ui.router.state.$state#transitionTo
927 * @methodOf ui.router.state.$state
930 * Low-level method for transitioning to a new state. {@link ui.router.state.$state#methods_go $state.go}
931 * uses `transitionTo` internally. `$state.go` is recommended in most situations.
935 * var app = angular.module('app', ['ui.router']);
937 * app.controller('ctrl', function ($scope, $state) {
938 * $scope.changeState = function () {
939 * $state.transitionTo('contact.detail');
944 * @param {string} to State name.
945 * @param {object=} toParams A map of the parameters that will be sent to the state,
946 * will populate $stateParams.
947 * @param {object=} options Options object. The options are:
949 * - **`location`** - {boolean=true|string=} - If `true` will update the url in the location bar, if `false`
950 * will not. If string, must be `"replace"`, which will update url and also replace last history record.
951 * - **`inherit`** - {boolean=false}, If `true` will inherit url parameters from current url.
952 * - **`relative`** - {object=}, When transitioning with relative path (e.g '^'),
953 * defines which state to be relative from.
954 * - **`notify`** - {boolean=true}, If `true` will broadcast $stateChangeStart and $stateChangeSuccess events.
955 * - **`reload`** (v0.2.5) - {boolean=false|string=|object=}, If `true` will force transition even if the state or params
956 * have not changed, aka a reload of the same state. It differs from reloadOnSearch because you'd
957 * use this when you want to force a reload when *everything* is the same, including search params.
958 * if String, then will reload the state with the name given in reload, and any children.
959 * if Object, then a stateObj is expected, will reload the state found in stateObj, and any children.
961 * @returns {promise} A promise representing the state of the new transition. See
962 * {@link ui.router.state.$state#methods_go $state.go}.
964 $state.transitionTo = function transitionTo(to, toParams, options) {
965 toParams = toParams || {};
967 location: true, inherit: false, relative: null, notify: true, reload: false, $retry: false
970 var from = $state.$current, fromParams = $state.params, fromPath = from.path;
971 var evt, toState = findState(to, options.relative);
973 // Store the hash param for later (since it will be stripped out by various methods)
974 var hash = toParams['#'];
976 if (!isDefined(toState)) {
977 var redirect = { to: to, toParams: toParams, options: options };
978 var redirectResult = handleRedirect(redirect, from.self, fromParams, options);
980 if (redirectResult) {
981 return redirectResult;
984 // Always retry once if the $stateNotFound was not prevented
985 // (handles either redirect changed or state lazy-definition)
987 toParams = redirect.toParams;
988 options = redirect.options;
989 toState = findState(to, options.relative);
991 if (!isDefined(toState)) {
992 if (!options.relative) throw new Error("No such state '" + to + "'");
993 throw new Error("Could not resolve '" + to + "' from state '" + options.relative + "'");
996 if (toState[abstractKey]) throw new Error("Cannot transition to abstract state '" + to + "'");
997 if (options.inherit) toParams = inheritParams($stateParams, toParams || {}, $state.$current, toState);
998 if (!toState.params.$$validates(toParams)) return TransitionFailed;
1000 toParams = toState.params.$$values(toParams);
1003 var toPath = to.path;
1005 // Starting from the root of the path, keep all levels that haven't changed
1006 var keep = 0, state = toPath[keep], locals = root.locals, toLocals = [];
1008 if (!options.reload) {
1009 while (state && state === fromPath[keep] && state.ownParams.$$equals(toParams, fromParams)) {
1010 locals = toLocals[keep] = state.locals;
1012 state = toPath[keep];
1014 } else if (isString(options.reload) || isObject(options.reload)) {
1015 if (isObject(options.reload) && !options.reload.name) {
1016 throw new Error('Invalid reload state object');
1019 var reloadState = options.reload === true ? fromPath[0] : findState(options.reload);
1020 if (options.reload && !reloadState) {
1021 throw new Error("No such reload state '" + (isString(options.reload) ? options.reload : options.reload.name) + "'");
1024 while (state && state === fromPath[keep] && state !== reloadState) {
1025 locals = toLocals[keep] = state.locals;
1027 state = toPath[keep];
1031 // If we're going to the same state and all locals are kept, we've got nothing to do.
1032 // But clear 'transition', as we still want to cancel any other pending transitions.
1033 // TODO: We may not want to bump 'transition' if we're called from a location change
1034 // that we've initiated ourselves, because we might accidentally abort a legitimate
1035 // transition initiated from code?
1036 if (shouldSkipReload(to, toParams, from, fromParams, locals, options)) {
1037 if (hash) toParams['#'] = hash;
1038 $state.params = toParams;
1039 copy($state.params, $stateParams);
1040 copy(filterByKeys(to.params.$$keys(), $stateParams), to.locals.globals.$stateParams);
1041 if (options.location && to.navigable && to.navigable.url) {
1042 $urlRouter.push(to.navigable.url, toParams, {
1043 $$avoidResync: true, replace: options.location === 'replace'
1045 $urlRouter.update(true);
1047 $state.transition = null;
1048 return $q.when($state.current);
1051 // Filter parameters before we pass them to event handlers etc.
1052 toParams = filterByKeys(to.params.$$keys(), toParams || {});
1054 // Re-add the saved hash before we start returning things or broadcasting $stateChangeStart
1055 if (hash) toParams['#'] = hash;
1057 // Broadcast start event and cancel the transition if requested
1058 if (options.notify) {
1061 * @name ui.router.state.$state#$stateChangeStart
1062 * @eventOf ui.router.state.$state
1063 * @eventType broadcast on root scope
1065 * Fired when the state transition **begins**. You can use `event.preventDefault()`
1066 * to prevent the transition from happening and then the transition promise will be
1067 * rejected with a `'transition prevented'` value.
1069 * @param {Object} event Event object.
1070 * @param {State} toState The state being transitioned to.
1071 * @param {Object} toParams The params supplied to the `toState`.
1072 * @param {State} fromState The current state, pre-transition.
1073 * @param {Object} fromParams The params supplied to the `fromState`.
1078 * $rootScope.$on('$stateChangeStart',
1079 * function(event, toState, toParams, fromState, fromParams){
1080 * event.preventDefault();
1081 * // transitionTo() promise will be rejected with
1082 * // a 'transition prevented' error
1086 if ($rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams, options).defaultPrevented) {
1087 $rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams);
1088 //Don't update and resync url if there's been a new transition started. see issue #2238, #600
1089 if ($state.transition == null) $urlRouter.update();
1090 return TransitionPrevented;
1094 // Resolve locals for the remaining states, but don't update any global state just
1095 // yet -- if anything fails to resolve the current state needs to remain untouched.
1096 // We also set up an inheritance chain for the locals here. This allows the view directive
1097 // to quickly look up the correct definition for each view in the current state. Even
1098 // though we create the locals object itself outside resolveState(), it is initially
1099 // empty and gets filled asynchronously. We need to keep track of the promise for the
1100 // (fully resolved) current locals, and pass this down the chain.
1101 var resolved = $q.when(locals);
1103 for (var l = keep; l < toPath.length; l++, state = toPath[l]) {
1104 locals = toLocals[l] = inherit(locals);
1105 resolved = resolveState(state, toParams, state === to, resolved, locals, options);
1108 // Once everything is resolved, we are ready to perform the actual transition
1109 // and return a promise for the new state. We also keep track of what the
1110 // current promise is, so that we can detect overlapping transitions and
1111 // keep only the outcome of the last transition.
1112 var transition = $state.transition = resolved.then(function () {
1113 var l, entering, exiting;
1115 if ($state.transition !== transition) return TransitionSuperseded;
1117 // Exit 'from' states not kept
1118 for (l = fromPath.length - 1; l >= keep; l--) {
1119 exiting = fromPath[l];
1120 if (exiting.self.onExit) {
1121 $injector.invoke(exiting.self.onExit, exiting.self, exiting.locals.globals);
1123 exiting.locals = null;
1126 // Enter 'to' states not kept
1127 for (l = keep; l < toPath.length; l++) {
1128 entering = toPath[l];
1129 entering.locals = toLocals[l];
1130 if (entering.self.onEnter) {
1131 $injector.invoke(entering.self.onEnter, entering.self, entering.locals.globals);
1135 // Run it again, to catch any transitions in callbacks
1136 if ($state.transition !== transition) return TransitionSuperseded;
1138 // Update globals in $state
1139 $state.$current = to;
1140 $state.current = to.self;
1141 $state.params = toParams;
1142 copy($state.params, $stateParams);
1143 $state.transition = null;
1145 if (options.location && to.navigable) {
1146 $urlRouter.push(to.navigable.url, to.navigable.locals.globals.$stateParams, {
1147 $$avoidResync: true, replace: options.location === 'replace'
1151 if (options.notify) {
1154 * @name ui.router.state.$state#$stateChangeSuccess
1155 * @eventOf ui.router.state.$state
1156 * @eventType broadcast on root scope
1158 * Fired once the state transition is **complete**.
1160 * @param {Object} event Event object.
1161 * @param {State} toState The state being transitioned to.
1162 * @param {Object} toParams The params supplied to the `toState`.
1163 * @param {State} fromState The current state, pre-transition.
1164 * @param {Object} fromParams The params supplied to the `fromState`.
1166 $rootScope.$broadcast('$stateChangeSuccess', to.self, toParams, from.self, fromParams);
1168 $urlRouter.update(true);
1170 return $state.current;
1171 }, function (error) {
1172 if ($state.transition !== transition) return TransitionSuperseded;
1174 $state.transition = null;
1177 * @name ui.router.state.$state#$stateChangeError
1178 * @eventOf ui.router.state.$state
1179 * @eventType broadcast on root scope
1181 * Fired when an **error occurs** during transition. It's important to note that if you
1182 * have any errors in your resolve functions (javascript errors, non-existent services, etc)
1183 * they will not throw traditionally. You must listen for this $stateChangeError event to
1184 * catch **ALL** errors.
1186 * @param {Object} event Event object.
1187 * @param {State} toState The state being transitioned to.
1188 * @param {Object} toParams The params supplied to the `toState`.
1189 * @param {State} fromState The current state, pre-transition.
1190 * @param {Object} fromParams The params supplied to the `fromState`.
1191 * @param {Error} error The resolve error object.
1193 evt = $rootScope.$broadcast('$stateChangeError', to.self, toParams, from.self, fromParams, error);
1195 if (!evt.defaultPrevented) {
1196 $urlRouter.update();
1199 return $q.reject(error);
1207 * @name ui.router.state.$state#is
1208 * @methodOf ui.router.state.$state
1211 * Similar to {@link ui.router.state.$state#methods_includes $state.includes},
1212 * but only checks for the full state name. If params is supplied then it will be
1213 * tested for strict equality against the current active params object, so all params
1214 * must match with none missing and no extras.
1218 * $state.$current.name = 'contacts.details.item';
1221 * $state.is('contact.details.item'); // returns true
1222 * $state.is(contactDetailItemStateObject); // returns true
1224 * // relative name (. and ^), typically from a template
1225 * // E.g. from the 'contacts.details' template
1226 * <div ng-class="{highlighted: $state.is('.item')}">Item</div>
1229 * @param {string|object} stateOrName The state name (absolute or relative) or state object you'd like to check.
1230 * @param {object=} params A param object, e.g. `{sectionId: section.id}`, that you'd like
1231 * to test against the current active state.
1232 * @param {object=} options An options object. The options are:
1234 * - **`relative`** - {string|object} - If `stateOrName` is a relative state name and `options.relative` is set, .is will
1235 * test relative to `options.relative` state (or name).
1237 * @returns {boolean} Returns true if it is the state.
1239 $state.is = function is(stateOrName, params, options) {
1240 options = extend({ relative: $state.$current }, options || {});
1241 var state = findState(stateOrName, options.relative);
1243 if (!isDefined(state)) { return undefined; }
1244 if ($state.$current !== state) { return false; }
1245 return params ? equalForKeys(state.params.$$values(params), $stateParams) : true;
1250 * @name ui.router.state.$state#includes
1251 * @methodOf ui.router.state.$state
1254 * A method to determine if the current active state is equal to or is the child of the
1255 * state stateName. If any params are passed then they will be tested for a match as well.
1256 * Not all the parameters need to be passed, just the ones you'd like to test for equality.
1259 * Partial and relative names
1261 * $state.$current.name = 'contacts.details.item';
1263 * // Using partial names
1264 * $state.includes("contacts"); // returns true
1265 * $state.includes("contacts.details"); // returns true
1266 * $state.includes("contacts.details.item"); // returns true
1267 * $state.includes("contacts.list"); // returns false
1268 * $state.includes("about"); // returns false
1270 * // Using relative names (. and ^), typically from a template
1271 * // E.g. from the 'contacts.details' template
1272 * <div ng-class="{highlighted: $state.includes('.item')}">Item</div>
1275 * Basic globbing patterns
1277 * $state.$current.name = 'contacts.details.item.url';
1279 * $state.includes("*.details.*.*"); // returns true
1280 * $state.includes("*.details.**"); // returns true
1281 * $state.includes("**.item.**"); // returns true
1282 * $state.includes("*.details.item.url"); // returns true
1283 * $state.includes("*.details.*.url"); // returns true
1284 * $state.includes("*.details.*"); // returns false
1285 * $state.includes("item.**"); // returns false
1288 * @param {string} stateOrName A partial name, relative name, or glob pattern
1289 * to be searched for within the current state name.
1290 * @param {object=} params A param object, e.g. `{sectionId: section.id}`,
1291 * that you'd like to test against the current active state.
1292 * @param {object=} options An options object. The options are:
1294 * - **`relative`** - {string|object=} - If `stateOrName` is a relative state reference and `options.relative` is set,
1295 * .includes will test relative to `options.relative` state (or name).
1297 * @returns {boolean} Returns true if it does include the state
1299 $state.includes = function includes(stateOrName, params, options) {
1300 options = extend({ relative: $state.$current }, options || {});
1301 if (isString(stateOrName) && isGlob(stateOrName)) {
1302 if (!doesStateMatchGlob(stateOrName)) {
1305 stateOrName = $state.$current.name;
1308 var state = findState(stateOrName, options.relative);
1309 if (!isDefined(state)) { return undefined; }
1310 if (!isDefined($state.$current.includes[state.name])) { return false; }
1311 return params ? equalForKeys(state.params.$$values(params), $stateParams, objectKeys(params)) : true;
1317 * @name ui.router.state.$state#href
1318 * @methodOf ui.router.state.$state
1321 * A url generation method that returns the compiled url for the given state populated with the given params.
1325 * expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
1328 * @param {string|object} stateOrName The state name or state object you'd like to generate a url from.
1329 * @param {object=} params An object of parameter values to fill the state's required parameters.
1330 * @param {object=} options Options object. The options are:
1332 * - **`lossy`** - {boolean=true} - If true, and if there is no url associated with the state provided in the
1333 * first parameter, then the constructed href url will be built from the first navigable ancestor (aka
1334 * ancestor with a valid url).
1335 * - **`inherit`** - {boolean=true}, If `true` will inherit url parameters from current url.
1336 * - **`relative`** - {object=$state.$current}, When transitioning with relative path (e.g '^'),
1337 * defines which state to be relative from.
1338 * - **`absolute`** - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl".
1340 * @returns {string} compiled state url
1342 $state.href = function href(stateOrName, params, options) {
1347 relative: $state.$current
1350 var state = findState(stateOrName, options.relative);
1352 if (!isDefined(state)) return null;
1353 if (options.inherit) params = inheritParams($stateParams, params || {}, $state.$current, state);
1355 var nav = (state && options.lossy) ? state.navigable : state;
1357 if (!nav || nav.url === undefined || nav.url === null) {
1360 return $urlRouter.href(nav.url, filterByKeys(state.params.$$keys().concat('#'), params || {}), {
1361 absolute: options.absolute
1367 * @name ui.router.state.$state#get
1368 * @methodOf ui.router.state.$state
1371 * Returns the state configuration object for any specific state or all states.
1373 * @param {string|object=} stateOrName (absolute or relative) If provided, will only get the config for
1374 * the requested state. If not provided, returns an array of ALL state configs.
1375 * @param {string|object=} context When stateOrName is a relative state reference, the state will be retrieved relative to context.
1376 * @returns {Object|Array} State configuration object or array of all objects.
1378 $state.get = function (stateOrName, context) {
1379 if (arguments.length === 0) return map(objectKeys(states), function(name) { return states[name].self; });
1380 var state = findState(stateOrName, context || $state.$current);
1381 return (state && state.self) ? state.self : null;
1384 function resolveState(state, params, paramsAreFiltered, inherited, dst, options) {
1385 // Make a restricted $stateParams with only the parameters that apply to this state if
1386 // necessary. In addition to being available to the controller and onEnter/onExit callbacks,
1387 // we also need $stateParams to be available for any $injector calls we make during the
1388 // dependency resolution process.
1389 var $stateParams = (paramsAreFiltered) ? params : filterByKeys(state.params.$$keys(), params);
1390 var locals = { $stateParams: $stateParams };
1392 // Resolve 'global' dependencies for the state, i.e. those not specific to a view.
1393 // We're also including $stateParams in this; that way the parameters are restricted
1394 // to the set that should be visible to the state, and are independent of when we update
1395 // the global $state and $stateParams values.
1396 dst.resolve = $resolve.resolve(state.resolve, locals, dst.resolve, state);
1397 var promises = [dst.resolve.then(function (globals) {
1398 dst.globals = globals;
1400 if (inherited) promises.push(inherited);
1402 function resolveViews() {
1403 var viewsPromises = [];
1405 // Resolve template and dependencies for all views.
1406 forEach(state.views, function (view, name) {
1407 var injectables = (view.resolve && view.resolve !== state.resolve ? view.resolve : {});
1408 injectables.$template = [ function () {
1409 return $view.load(name, { view: view, locals: dst.globals, params: $stateParams, notify: options.notify }) || '';
1412 viewsPromises.push($resolve.resolve(injectables, dst.globals, dst.resolve, state).then(function (result) {
1413 // References to the controller (only instantiated at link time)
1414 if (isFunction(view.controllerProvider) || isArray(view.controllerProvider)) {
1415 var injectLocals = angular.extend({}, injectables, dst.globals);
1416 result.$$controller = $injector.invoke(view.controllerProvider, null, injectLocals);
1418 result.$$controller = view.controller;
1420 // Provide access to the state itself for internal use
1421 result.$$state = state;
1422 result.$$controllerAs = view.controllerAs;
1427 return $q.all(viewsPromises).then(function(){
1432 // Wait for all the promises and then return the activation object
1433 return $q.all(promises).then(resolveViews).then(function (values) {
1441 function shouldSkipReload(to, toParams, from, fromParams, locals, options) {
1442 // Return true if there are no differences in non-search (path/object) params, false if there are differences
1443 function nonSearchParamsEqual(fromAndToState, fromParams, toParams) {
1444 // Identify whether all the parameters that differ between `fromParams` and `toParams` were search params.
1445 function notSearchParam(key) {
1446 return fromAndToState.params[key].location != "search";
1448 var nonQueryParamKeys = fromAndToState.params.$$keys().filter(notSearchParam);
1449 var nonQueryParams = pick.apply({}, [fromAndToState.params].concat(nonQueryParamKeys));
1450 var nonQueryParamSet = new $$UMFP.ParamSet(nonQueryParams);
1451 return nonQueryParamSet.$$equals(fromParams, toParams);
1454 // If reload was not explicitly requested
1455 // and we're transitioning to the same state we're already in
1456 // and the locals didn't change
1457 // or they changed in a way that doesn't merit reloading
1458 // (reloadOnParams:false, or reloadOnSearch.false and only search params changed)
1459 // Then return true.
1460 if (!options.reload && to === from &&
1461 (locals === from.locals || (to.self.reloadOnSearch === false && nonSearchParamsEqual(from, fromParams, toParams)))) {
1467 angular.module('ui.router.state')
1468 .factory('$stateParams', function () { return {}; })
1469 .provider('$state', $StateProvider);