You can use resolve
to provide your controller with data before it loads the next state. To access the resolved objects, you will need to inject them into the controller as dependencies.
Let's use a shopping list application as an example. We'll start by defining our application module, and including ui.router
as a dependency.:
angular.module('myApp', ['ui.router']);
We now want to define the module that will be specific to the shopping list page of our application. We'll define a shoppingList
module, include the states for that module, a resolve for that state, and the controller.
Shopping List Module
angular.module('myApp.shoppingList').config(function ($stateProvider) {
$stateProvider.state('app.shoppingList', {
url: '/shopping-list',
templateUrl: 'shopping-list.html',
controller: 'ShoppingListController',
resolve: {
shoppingLists: function (ShoppingListService) {
return ShoppingListService.getAll();
}
}
});
});
We now can inject our resolved objects into our controller as dependencies. In the above state, I am resolving an object to the name shoppingLists
. If I want to use this object in my controller, I include it as a dependency with the same name.
Shopping List Controller
angular.module('myApp.shoppingList').controller('ShoppingListController', function ($scope, shoppingLists) {
$scope.shoppingLists = shoppingLists;
});
For additional details read the Angular-UI Wiki, which includes an in-depth guide to using resolve.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…