State Changes due to user action
Controller for that state is instantiated
State Changes
Controller starts
$stateProvider
.state('todos', {
url: '/',
controller: "TodosController",
templateUrl: "templates/index.html"
});
app.controller("TodosController",
function($scope, TodoService){
TodoService.getTodos()
.then(function(todos){
$scope.todos = todos.data;
}).catch(function(err){
$scope.errors = err;
});
});
State Changes
Controller starts
$stateProvider
.state('todos', {
url: '/',
controller: "TodosController",
templateUrl: "templates/index.html"
});
app.controller("TodosController",
function($scope, TodoService){
TodoService.getTodos()
.then(function(todos){
$scope.todos = todos.data;
}).catch(function(err){
$scope.errors = err;
});
});
Data Fetched and returned
State Changes
Controller starts
Data Fetched and returned
The problem is that if this is slow, our user will see flickering as the two way data-bindings update.
Especially if there are multiple requests.
State Changes
Resolves are evaluated
Controller starts
Data Fetched and returned
State Changes
Resolves are evaluated
Controller starts
$stateProvider
.state('todos', {
url: '/',
controller: "TodosController",
templateUrl: "templates/index.html",
resolve: {
TodoData: function(TodoService) {
return TodoService.getTodos();
}
}
});
app.controller("TodosController",
function($scope, TodoData){
$scope.todos = TodoData;
});
State Changes
Resolves are evaluated
Controller starts
Now when our controller tries to instantiate we already have all the data
$stateProvider
.state('todos', {
url: '/',
controller: "TodosController",
templateUrl: "templates/index.html",
resolve: {
// What goes here?
}
});
app.controller("TodosController",
function($scope){
});
Notice the property 'resolve'.
This must always be an object.
$stateProvider
.state('todos', {
url: '/',
controller: "TodosController",
templateUrl: "templates/index.html",
resolve: {
TodoData: // What goes here?
}
});
app.controller("TodosController",
function($scope, TodoData){
$scope.todos = TodoData;
});
Notice that the property key is also the value we can inject into our controller
$stateProvider
.state('todos', {
url: '/',
controller: "TodosController",
templateUrl: "templates/index.html",
resolve: {
TodoData: function(TodoService) {
return TodoService.getTodos();
}
}
});
app.controller("TodosController",
function($scope, TodoData){
$scope.todos = TodoData;
});
Resolves must be either a function or a string. Functions are evaluated, and the return value is what gets injected.
$stateProvider
.state('todos', {
url: '/',
controller: "TodosController",
templateUrl: "templates/index.html",
resolve: {
TodoData: function(TodoService) {
return TodoService.getTodos();
}
}
});
app.controller("TodosController",
function($scope, TodoData){
$scope.todos = TodoData;
});
If your resolve is a function, you can use dependency injection just like it were a controller or service.
$stateProvider
.state('todos', {
url: '/',
controller: "TodosController",
templateUrl: "templates/index.html",
resolve: {
TodoData: function(TodoService) {
return TodoService.getTodos();
}
}
});
app.controller("TodosController",
function($scope, TodoData){
$scope.todos = TodoData;
});
TodoService.getTodos() returns a promise. In our controlelr TodoData is the value of the resolved promise
$stateProvider
.state('todos', {
url: '/',
controller: "TodosController",
templateUrl: "templates/index.html",
resolve: {
TodoData: 'TodoService'
}
});
app.controller("TodosController",
function($scope, TodoData, TodoService){
TodoService === TodoData; // true
});
I don't find this to be as useful, since you can easily inject services directly into your controller.
https://github.com/gSchool/angular-curriculum/tree/master/Unit-3/examples/ui_router_crud_resolves