I'm quite new to AngularJS, I'm struggling with following problem for a while now:
I want to use the resolve functionality of the routeProvider to load some data via a service. But always end up with this error:
Uncaught Error: [$injector:modulerr] Failed to instantiate module footballmvc due to: Error: [$injector:modulerr] Failed to instantiate module teams due to: Error: [$injector:unpr] Unknown provider: Teams
This is my code:
index.html
...
<script src="app/app.js"></script>
<script src="app/config.js"></script>
<script src="app/services/divisionService.js"></script>
<script src="app/services/teamService.js"></script>
<script src="app/teams/teamControllers.js"></script>
<script src="app/divisions/divisionControllers.js"></script>
...
...
<body ng-app="footballmvc">
<div class="container-fluid">
<div class="row" id="header">
<h1>{{title}}</h1>
</div>
<div class="row">
<div id="menu" class="col-lg-2">
<ul class="list-unstyled">
<li><a href="#/divisions"><h3>Divisions</h3></a></li>
<li><a href="#/teams"><h3>Teams</h3></a></li>
</ul>
</div>
<div id="content" class="col-lg-10" ng-view>
<!-- Content goes here! -->
</div>
</div>
</div>
</body>
...
app.js
var app = angular.module('footballmvc', ['config','ngRoute','teams','divisions'])
...
teamControllers.js
angular.module('teams', [])
.config(function($routeProvider, Teams, Divisions) {
$routeProvider
.when('/teams', {
controller:'TeamListCtrl',
templateUrl:'app/teams/list.tpl.html',
resolve: {
teams : function(Teams) {
return Teams.query();
}
}
})
...
teamService.js
angular.module('teams', [])
.factory('Teams', function($resource, config){
return $resource(config.MONGO_URL + 'teams/:id', {apiKey: config.MONGO_API_KEY, id:'@_id.$oid'});
});
***UPDATE: SOLUTION***
Finally found the solution. Clue is to pass the dependency to the service only to the resolve function and not to the config function:
config(function ($routeProvider) {
$routeProvider
.when('/teams', {
controller: 'TeamListCtrl',
templateUrl: 'app/teams/list.tpl.html',
resolve: {
teams: function (Teams) {
return Teams.query();
}
}
})