Justin Young
Justin Young

Reputation: 2463

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?

Upvotes: 1

Views: 1695

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

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
})

Upvotes: 3

Related Questions