Skip to content

Commit 153fe27

Browse files
committed
Merge pull request johnpapa#202 from johnpapa/develop
added an example of a named reoute resolve function
2 parents 6d79816 + 249337f commit 153fe27

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
@@ -1290,7 +1290,7 @@ While this guide explains the *what*, *why* and *how*, I find it helpful to see
12901290
### Route Resolve Promises
12911291
###### [Style [Y081](#style-y081)]
12921292
1293-
- 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.
1293+
- 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.
12941294
12951295
*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.
12961296
@@ -1345,6 +1345,43 @@ While this guide explains the *what*, *why* and *how*, I find it helpful to see
13451345
}
13461346
```
13471347
1348+
Note: The example below shows the route resolve points to a named function, which is easier to debug and easier to handle dependency injection.
1349+
1350+
```javascript
1351+
/* even better */
1352+
1353+
// route-config.js
1354+
angular
1355+
.module('app')
1356+
.config(config);
1357+
1358+
function config($routeProvider) {
1359+
$routeProvider
1360+
.when('/avengers', {
1361+
templateUrl: 'avengers.html',
1362+
controller: 'Avengers',
1363+
controllerAs: 'vm',
1364+
resolve: {
1365+
moviesPrepService: moviesPrepService
1366+
}
1367+
});
1368+
}
1369+
1370+
function(movieService) {
1371+
return movieService.getMovies();
1372+
}
1373+
1374+
// avengers.js
1375+
angular
1376+
.module('app')
1377+
.controller('Avengers', Avengers);
1378+
1379+
Avengers.$inject = ['moviesPrepService'];
1380+
function Avengers(moviesPrepService) {
1381+
var vm = this;
1382+
vm.movies = moviesPrepService.movies;
1383+
}
1384+
```
13481385
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).
13491386
13501387
**[Back to top](#table-of-contents)**

0 commit comments

Comments
 (0)