Thursday, October 13, 2016

AngularJS - Route Resolve

The resolve property is used either inside the .when() method of $routeProvider service or .state() method of $stateProvider service. The use of resolve is to make sure that the route transitioning happens only when promises contained inside the resolve property are resolved completely. This is extremely useful in scenarios where the database, services and application are hosted on different servers and there is network latency in getting data. The data that is retrieved from the resolve is stored in user defined property, which can be injected in controllers. This means controller doesn't have to fetch the data by itself.

The resolve property is a map object. The map object contains key/value pairs of:
key - {string}: a name of a dependency to be injected into the controller.
value - {string|function}:
If string, then it is an alias for a service. Otherwise if function, then it is injected and the return value is treated as the dependency. If the result is a promise, it is resolved before the controller is instantiated and its value is injected into the controller.

For more details please follow the ui-router wiki. 



No comments:

Post a Comment