You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-1Lines changed: 38 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1272,7 +1272,7 @@ While this guide explains the *what*, *why* and *how*, I find it helpful to see
1272
1272
### Route Resolve Promises
1273
1273
###### [Style [Y081](#style-y081)]
1274
1274
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.
1276
1276
1277
1277
*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.
1278
1278
@@ -1327,6 +1327,43 @@ While this guide explains the *what*, *why* and *how*, I find it helpful to see
1327
1327
}
1328
1328
```
1329
1329
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
+
functionconfig($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
+
returnmovieService.getMovies();
1354
+
}
1355
+
1356
+
// avengers.js
1357
+
angular
1358
+
.module('app')
1359
+
.controller('Avengers', Avengers);
1360
+
1361
+
Avengers.$inject= ['moviesPrepService'];
1362
+
functionAvengers(moviesPrepService) {
1363
+
var vm =this;
1364
+
vm.movies=moviesPrepService.movies;
1365
+
}
1366
+
```
1330
1367
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).
0 commit comments