UNPKG

14.6 kBTypeScriptView Raw
1import { HookResult, TransitionOptions } from '../transition/interface';
2import { Transition } from '../transition/transition';
3import { HrefOptions, LazyLoadResult, StateDeclaration, StateOrName, TransitionPromise } from './interface';
4import { StateObject } from './stateObject';
5import { TargetState } from './targetState';
6import { RawParams } from '../params/interface';
7import { UIRouter } from '../router';
8import { UIInjector } from '../interface';
9import { StateParams } from '../params/stateParams';
10export declare type OnInvalidCallback = (toState?: TargetState, fromState?: TargetState, injector?: UIInjector) => HookResult;
11/**
12 * Provides services related to ui-router states.
13 *
14 * This API is located at `router.stateService` ([[UIRouter.stateService]])
15 */
16export declare class StateService {
17 private router;
18 /** @internal */
19 invalidCallbacks: OnInvalidCallback[];
20 /**
21 * The [[Transition]] currently in progress (or null)
22 *
23 * @deprecated This is a passthrough through to [[UIRouterGlobals.transition]]
24 */
25 transition: Transition;
26 /**
27 * The latest successful state parameters
28 *
29 * @deprecated This is a passthrough through to [[UIRouterGlobals.params]]
30 */
31 params: StateParams;
32 /**
33 * The current [[StateDeclaration]]
34 *
35 * @deprecated This is a passthrough through to [[UIRouterGlobals.current]]
36 */
37 current: StateDeclaration;
38 /**
39 * The current [[StateObject]] (an internal API)
40 *
41 * @deprecated This is a passthrough through to [[UIRouterGlobals.$current]]
42 */
43 $current: StateObject;
44 /** @internal */
45 constructor(/** @internal */ router: UIRouter);
46 /** @internal */
47 dispose(): void;
48 /**
49 * Handler for when [[transitionTo]] is called with an invalid state.
50 *
51 * Invokes the [[onInvalid]] callbacks, in natural order.
52 * Each callback's return value is checked in sequence until one of them returns an instance of TargetState.
53 * The results of the callbacks are wrapped in $q.when(), so the callbacks may return promises.
54 *
55 * If a callback returns an TargetState, then it is used as arguments to $state.transitionTo() and the result returned.
56 *
57 * @internal
58 */
59 private _handleInvalidTargetState;
60 /**
61 * Registers an Invalid State handler
62 *
63 * Registers a [[OnInvalidCallback]] function to be invoked when [[StateService.transitionTo]]
64 * has been called with an invalid state reference parameter
65 *
66 * Example:
67 * ```js
68 * stateService.onInvalid(function(to, from, injector) {
69 * if (to.name() === 'foo') {
70 * let lazyLoader = injector.get('LazyLoadService');
71 * return lazyLoader.load('foo')
72 * .then(() => stateService.target('foo'));
73 * }
74 * });
75 * ```
76 *
77 * @param {function} callback invoked when the toState is invalid
78 * This function receives the (invalid) toState, the fromState, and an injector.
79 * The function may optionally return a [[TargetState]] or a Promise for a TargetState.
80 * If one is returned, it is treated as a redirect.
81 *
82 * @returns a function which deregisters the callback
83 */
84 onInvalid(callback: OnInvalidCallback): Function;
85 /**
86 * Reloads the current state
87 *
88 * A method that force reloads the current state, or a partial state hierarchy.
89 * All resolves are re-resolved, and components reinstantiated.
90 *
91 * #### Example:
92 * ```js
93 * let app angular.module('app', ['ui.router']);
94 *
95 * app.controller('ctrl', function ($scope, $state) {
96 * $scope.reload = function(){
97 * $state.reload();
98 * }
99 * });
100 * ```
101 *
102 * Note: `reload()` is just an alias for:
103 *
104 * ```js
105 * $state.transitionTo($state.current, $state.params, {
106 * reload: true, inherit: false
107 * });
108 * ```
109 *
110 * @param reloadState A state name or a state object.
111 * If present, this state and all its children will be reloaded, but ancestors will not reload.
112 *
113 * #### Example:
114 * ```js
115 * //assuming app application consists of 3 states: 'contacts', 'contacts.detail', 'contacts.detail.item'
116 * //and current state is 'contacts.detail.item'
117 * let app angular.module('app', ['ui.router']);
118 *
119 * app.controller('ctrl', function ($scope, $state) {
120 * $scope.reload = function(){
121 * //will reload 'contact.detail' and nested 'contact.detail.item' states
122 * $state.reload('contact.detail');
123 * }
124 * });
125 * ```
126 *
127 * @returns A promise representing the state of the new transition. See [[StateService.go]]
128 */
129 reload(reloadState?: StateOrName): Promise<StateObject>;
130 /**
131 * Transition to a different state and/or parameters
132 *
133 * Convenience method for transitioning to a new state.
134 *
135 * `$state.go` calls `$state.transitionTo` internally but automatically sets options to
136 * `{ location: true, inherit: true, relative: router.globals.$current, notify: true }`.
137 * This allows you to use either an absolute or relative `to` argument (because of `relative: router.globals.$current`).
138 * It also allows you to specify * only the parameters you'd like to update, while letting unspecified parameters
139 * inherit from the current parameter values (because of `inherit: true`).
140 *
141 * #### Example:
142 * ```js
143 * let app = angular.module('app', ['ui.router']);
144 *
145 * app.controller('ctrl', function ($scope, $state) {
146 * $scope.changeState = function () {
147 * $state.go('contact.detail');
148 * };
149 * });
150 * ```
151 *
152 * @param to Absolute state name, state object, or relative state path (relative to current state).
153 *
154 * Some examples:
155 *
156 * - `$state.go('contact.detail')` - will go to the `contact.detail` state
157 * - `$state.go('^')` - will go to the parent state
158 * - `$state.go('^.sibling')` - if current state is `home.child`, will go to the `home.sibling` state
159 * - `$state.go('.child.grandchild')` - if current state is home, will go to the `home.child.grandchild` state
160 *
161 * @param params A map of the parameters that will be sent to the state, will populate $stateParams.
162 *
163 * Any parameters that are not specified will be inherited from current parameter values (because of `inherit: true`).
164 * This allows, for example, going to a sibling state that shares parameters defined by a parent state.
165 *
166 * @param options Transition options
167 *
168 * @returns {promise} A promise representing the state of the new transition.
169 */
170 go(to: StateOrName, params?: RawParams, options?: TransitionOptions): TransitionPromise;
171 /**
172 * Creates a [[TargetState]]
173 *
174 * This is a factory method for creating a TargetState
175 *
176 * This may be returned from a Transition Hook to redirect a transition, for example.
177 */
178 target(identifier: StateOrName, params?: RawParams, options?: TransitionOptions): TargetState;
179 /** @internal */
180 private getCurrentPath;
181 /**
182 * Low-level method for transitioning to a new state.
183 *
184 * The [[go]] method (which uses `transitionTo` internally) is recommended in most situations.
185 *
186 * #### Example:
187 * ```js
188 * let app = angular.module('app', ['ui.router']);
189 *
190 * app.controller('ctrl', function ($scope, $state) {
191 * $scope.changeState = function () {
192 * $state.transitionTo('contact.detail');
193 * };
194 * });
195 * ```
196 *
197 * @param to State name or state object.
198 * @param toParams A map of the parameters that will be sent to the state,
199 * will populate $stateParams.
200 * @param options Transition options
201 *
202 * @returns A promise representing the state of the new transition. See [[go]]
203 */
204 transitionTo(to: StateOrName, toParams?: RawParams, options?: TransitionOptions): TransitionPromise;
205 /**
206 * Checks if the current state *is* the provided state
207 *
208 * Similar to [[includes]] but only checks for the full state name.
209 * If params is supplied then it will be tested for strict equality against the current
210 * active params object, so all params must match with none missing and no extras.
211 *
212 * #### Example:
213 * ```js
214 * $state.$current.name = 'contacts.details.item';
215 *
216 * // absolute name
217 * $state.is('contact.details.item'); // returns true
218 * $state.is(contactDetailItemStateObject); // returns true
219 * ```
220 *
221 * // relative name (. and ^), typically from a template
222 * // E.g. from the 'contacts.details' template
223 * ```html
224 * <div ng-class="{highlighted: $state.is('.item')}">Item</div>
225 * ```
226 *
227 * @param stateOrName The state name (absolute or relative) or state object you'd like to check.
228 * @param params A param object, e.g. `{sectionId: section.id}`, that you'd like
229 * to test against the current active state.
230 * @param options An options object. The options are:
231 * - `relative`: If `stateOrName` is a relative state name and `options.relative` is set, .is will
232 * test relative to `options.relative` state (or name).
233 *
234 * @returns Returns true if it is the state.
235 */
236 is(stateOrName: StateOrName, params?: RawParams, options?: {
237 relative?: StateOrName;
238 }): boolean;
239 /**
240 * Checks if the current state *includes* the provided state
241 *
242 * A method to determine if the current active state is equal to or is the child of the
243 * state stateName. If any params are passed then they will be tested for a match as well.
244 * Not all the parameters need to be passed, just the ones you'd like to test for equality.
245 *
246 * #### Example when `$state.$current.name === 'contacts.details.item'`
247 * ```js
248 * // Using partial names
249 * $state.includes("contacts"); // returns true
250 * $state.includes("contacts.details"); // returns true
251 * $state.includes("contacts.details.item"); // returns true
252 * $state.includes("contacts.list"); // returns false
253 * $state.includes("about"); // returns false
254 * ```
255 *
256 * #### Glob Examples when `* $state.$current.name === 'contacts.details.item.url'`:
257 * ```js
258 * $state.includes("*.details.*.*"); // returns true
259 * $state.includes("*.details.**"); // returns true
260 * $state.includes("**.item.**"); // returns true
261 * $state.includes("*.details.item.url"); // returns true
262 * $state.includes("*.details.*.url"); // returns true
263 * $state.includes("*.details.*"); // returns false
264 * $state.includes("item.**"); // returns false
265 * ```
266 *
267 * @param stateOrName A partial name, relative name, glob pattern,
268 * or state object to be searched for within the current state name.
269 * @param params A param object, e.g. `{sectionId: section.id}`,
270 * that you'd like to test against the current active state.
271 * @param options An options object. The options are:
272 * - `relative`: If `stateOrName` is a relative state name and `options.relative` is set, .is will
273 * test relative to `options.relative` state (or name).
274 *
275 * @returns {boolean} Returns true if it does include the state
276 */
277 includes(stateOrName: StateOrName, params?: RawParams, options?: TransitionOptions): boolean;
278 /**
279 * Generates a URL for a state and parameters
280 *
281 * Returns the url for the given state populated with the given params.
282 *
283 * #### Example:
284 * ```js
285 * expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
286 * ```
287 *
288 * @param stateOrName The state name or state object you'd like to generate a url from.
289 * @param params An object of parameter values to fill the state's required parameters.
290 * @param options Options object. The options are:
291 *
292 * @returns {string} compiled state url
293 */
294 href(stateOrName: StateOrName, params?: RawParams, options?: HrefOptions): string;
295 /** @internal */
296 private _defaultErrorHandler;
297 /**
298 * Sets or gets the default [[transitionTo]] error handler.
299 *
300 * The error handler is called when a [[Transition]] is rejected or when any error occurred during the Transition.
301 * This includes errors caused by resolves and transition hooks.
302 *
303 * Note:
304 * This handler does not receive certain Transition rejections.
305 * Redirected and Ignored Transitions are not considered to be errors by [[StateService.transitionTo]].
306 *
307 * The built-in default error handler logs the error to the console.
308 *
309 * You can provide your own custom handler.
310 *
311 * #### Example:
312 * ```js
313 * stateService.defaultErrorHandler(function() {
314 * // Do not log transitionTo errors
315 * });
316 * ```
317 *
318 * @param handler a global error handler function
319 * @returns the current global error handler
320 */
321 defaultErrorHandler(handler?: (error: any) => void): (error: any) => void;
322 /**
323 * Gets a registered [[StateDeclaration]] object
324 *
325 * Returns the state declaration object for any specific state, or for all registered states.
326 *
327 * @param stateOrName (absolute or relative) If provided, will only get the declaration object for the requested state.
328 * If not provided, returns an array of ALL states.
329 * @param base When `stateOrName` is a relative state reference (such as `.bar.baz`), the state will be retrieved relative to this state.
330 *
331 * @returns a [[StateDeclaration]] object (or array of all registered [[StateDeclaration]] objects.)
332 * @deprecated use [[StateRegistry.get]]
333 */
334 get(stateOrName: StateOrName, base: StateOrName): StateDeclaration;
335 get(stateOrName: StateOrName): StateDeclaration;
336 get(): StateDeclaration[];
337 /**
338 * Lazy loads a state
339 *
340 * Explicitly runs a state's [[StateDeclaration.lazyLoad]] function.
341 *
342 * @param stateOrName the state that should be lazy loaded
343 * @param transition the optional Transition context to use (if the lazyLoad function requires an injector, etc)
344 * Note: If no transition is provided, a noop transition is created using the from the current state to the current state.
345 * This noop transition is not actually run.
346 *
347 * @returns a promise to lazy load
348 */
349 lazyLoad(stateOrName: StateOrName, transition?: Transition): Promise<LazyLoadResult>;
350}
351
\No newline at end of file