I would like to get some data before the view shows, (resolve). But some of the data is dependent on the result of another promise. I get the job id (index) from $stateParams
and look up the data in my service. Once completed, from this result (the job) I look up the settings and floors (each from a different service). Both return a promise.
I've came up with:
jobinfo: function(Jobs, Floor, JobSetting, $stateParams, $q) {
var defer = $q.defer();
Jobs.getByIndex($stateParams.index)
.then(function(job) {
console.log('got jobs');
$q.all({floors: Floor.getByJob(job), settings: JobSetting.getByJob(job)})
.then(function(info) {
console.log('got info');
defer.resolve([job, info.floors, info.settings]);
});
});
return defer.promise;
}
Take 2:
jobinfo: function(Jobs, Floor, JobSetting, $stateParams, $q) {
return Jobs.getByIndex($stateParams.index)
.then(function(job) {
console.log('got jobs');
return $q.all({floors: Floor.getByJob(job), settings: JobSetting.getByJob(job)})
.then(function(info) {
console.log('got info');
return [job, info.floors, info.settings];
});
});
}
Both fail. I do not even get a console.log
back. I left the rest of the code out, obviously they are wrapped in :
resolve: {
...
}
and defined in the right place.
I like to separate the resolvers.
You can inject the value on each resolver this way:
From ui-router documentation: