It is worth noting that resolve
can also accept an object. Each property within the object must be resolved before the controller is instantiated, thereby allowing the desired state to be achieved.
In the example below, all objects listed in the resolve section must be resolved (using deferred.resolve()
if they are promises) prior to the instantiation of the controller
. It is important to observe how each resolve object is injected as a parameter into the controller (UI-Router documentation on resolve).
Here is an example to demonstrate:
$stateProvider.state('myState', {
resolve: {
resolve0: function () { // resolve with a plain value
return 'somedata';
},
resolve1: function ($q) { // resolve with a promise
return $q.resolve('somedata');
},
resolve2: function ($q) { // reject with a promise, preventing the state from completing the change
return $q.reject('some error');
}
}
}