siannone
siannone

Reputation: 6763

Passing resolve result to controller in AngularJS fails

I set up the configuration for my main module in the following way:

angular.module('main').config(function($routeProvider, $locationProvider, ConfigProvider)
    {
        $routeProvider
            .when('/home',
            {
                templateUrl : ConfigProvider.path.views + '/page/home.html',
                controller : 'PageHomeCtrl',
                resolve : {
                    resolveHomeData : function(ResolveService) { return ResolveService.resolveHome() }
                }
            });

        $locationProvider.html5Mode(true);
    }
);

The ResolveService takes care of resolving the routes and at the moment is very simple:

angular.module('app.resolve').factory('ResolveService', function($q, UserService)
{
    return {
        resolveHome : function()
        {
            var deferred = $q.defer();

            UserService.find('me', function(data)
            {
                deferred.resolve(data);
            }, function(data, status)
            {
                deferred.reject(data);
            });

            return deferred.promise;
        }
    }
});

This instead is the PageHomeCtrl:

angular.module('app.ui.page.home').controller('PageHomeCtrl', function($q, $scope, UserService, resolveHomeData)
{

});

As you can see I'm injecting the resolveHomeData in it but I get the following error from AngularJS:

Unknown provider: resolveHomeDataProvider

Why is resolveHomeData not injected correctly into my controller?

I'm concatenating everything with a Gulp script and I'm including all AngularJS files in the following order:

  1. Modules definitions
  2. Modules configurations
  3. Providers
  4. Controllers
  5. Directives
  6. Filters
  7. Services

Edit:

This is the main module definition:

angular.module('main',
    [
        'ngRoute',
        'app.config',
        'app.resolve',
        'app.ui.page.index',
        'app.ui.page.home',
        'app.ui.modal.login'
    ]
)

Upvotes: 0

Views: 62

Answers (1)

Omri Aharon
Omri Aharon

Reputation: 17064

EDIT: deleted my answer before due to your comment.

AngularJS thinks your resolveHomeData is a provider/service instead of the resolved data. This happens because the controller is also defined in the HTML by accident, thus having twice the declaration for the controller: once in the HTML, and once in the config.

Remove the HTML and it should work.

Upvotes: 1

Related Questions