I am not able to get the data from resolve in ui-router, its giving me undefined, below is my code
Controller
angular
.module('App')
.controller('PostController', PostController);
/** @ngInject */
function PostController(questionData) {
var vm = this;
vm.post = questionData;
console.log("Post Data in Ctrl", questionData);
}
Factory
I have a Cached list of post in bootData, first I check if post with id is in cached array, if found return the post Object, if not make the request and return the promise.
function postService(dataService, $filter, $q) {
return {
post : post
}
// Cached promise of posts
var bootData = dataService.boot();
function post(id) {
bootData.success(function(res){
var postList = res.featured.posts.data;
var post = $filter('filter')(postList, {id: id})[0];
// Check if post with id found return it
if (post && post.id > 0) {
var deferred = $q.defer();
deferred.resolve(post);
return deferred.promise;
} else {
// Make request to api and return promise
return dataService.get('questions/' + id);
}
});
}
}
Route
resolve: {
// Check for logged in
loginRequired: function(auth){
return auth.loginRequired();
},
postData: function(postService, $stateParams) {
return postService.post($stateParams.id);
}
}
I am stuck here, I thing there something wrong in factory method post(id). Please help
if (post && post.id > 0) {conditional, my controller only takingpostDataand assigning it to scopevm.post, its returning undefinedconsole.log("Post Data in Ctrl", postData);