Justin Young Justin Young - 1 month ago 6
AngularJS Question

resolve resource in route

Trying to send my resolved data to my controller. Things resolve fine, just can't figure out how to pass it into my controller. Right now I get get undefined when I try and log out the resolved data.

My module:

angular.module('projects').config(['$stateProvider',
function($stateProvider) {
$stateProvider
.state('view', {
url: '/:projectId',
templateUrl: 'view.html',
controller:'ProjectsClientController',
resolve:{
projectaa:function(apiResource, $stateParams){
apiResource.get({api_resource:'projects', api_value:$stateParams.projectId}).$promise.then(function(response){
return response;
});

}
}
})
}
])



angular.module('projects').controller('ProjectsClientController', ['$scope', '$stateParams', 'projectaa',
function($scope, $stateParams, projectaa) {
console.log(projectaa);


}
]);


What am I doing wrong?

Answer

It should also return the promise of apiResource.get so that the promise chain .then function would be accessible inside the controller

Code

projectaa: function(apiResource, $stateParams) {
    return apiResource.get({
        api_resource: 'projects',
        api_value: $stateParams.projectId
    }).$promise;
}

Inside controller you could get data by resolving that promise using .then

Controller

projectaa.then(function(data){
   console.log(data); //you will get promise resolved here
   //which will return data here
})
Comments