Skip to content

Commit cfd9721

Browse files
committed
added an example of a named reoute resolve function
1 parent 19f935b commit cfd9721

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ While this guide explains the *what*, *why* and *how*, I find it helpful to see
12721272
### Route Resolve Promises
12731273
###### [Style [Y081](#style-y081)]
12741274
1275-
- When a controller depends on a promise to be resolved, resolve those dependencies in the `$routeProvider` before the controller logic is executed. If you need to conditionally cancel a route before the controller is activated, use a route resolver.
1275+
- When a controller depends on a promise to be resolved before the controller is activated, resolve those dependencies in the `$routeProvider` before the controller logic is executed. If you need to conditionally cancel a route before the controller is activated, use a route resolver.
12761276
12771277
*Why?*: A controller may require data before it loads. That data may come from a promise via a custom factory or [$http](https://docs.angularjs.org/api/ng/service/$http). Using a [route resolve](https://docs.angularjs.org/api/ngRoute/provider/$routeProvider) allows the promise to resolve before the controller logic executes, so it might take action based on that data from the promise.
12781278
@@ -1327,6 +1327,43 @@ While this guide explains the *what*, *why* and *how*, I find it helpful to see
13271327
}
13281328
```
13291329
1330+
Note: The example below shows the route resolve function becomes a named function, which is easier to debug and easier to handle dependency injection.
1331+
1332+
```javascript
1333+
/* even better */
1334+
1335+
// route-config.js
1336+
angular
1337+
.module('app')
1338+
.config(config);
1339+
1340+
function config($routeProvider) {
1341+
$routeProvider
1342+
.when('/avengers', {
1343+
templateUrl: 'avengers.html',
1344+
controller: 'Avengers',
1345+
controllerAs: 'vm',
1346+
resolve: {
1347+
moviesPrepService: moviesPrepService
1348+
}
1349+
});
1350+
}
1351+
1352+
function(movieService) {
1353+
return movieService.getMovies();
1354+
}
1355+
1356+
// avengers.js
1357+
angular
1358+
.module('app')
1359+
.controller('Avengers', Avengers);
1360+
1361+
Avengers.$inject = ['moviesPrepService'];
1362+
function Avengers(moviesPrepService) {
1363+
var vm = this;
1364+
vm.movies = moviesPrepService.movies;
1365+
}
1366+
```
13301367
Note: The code example's dependency on `movieService` is not minification safe on its own. For details on how to make this code minification safe, see the sections on [dependency injection](#manual-annotating-for-dependency-injection) and on [minification and annotation](#minification-and-annotation).
13311368
13321369
**[Back to top](#table-of-contents)**

0 commit comments

Comments
 (0)