I've come across a few examples of how to insert custom providers into angular's .config
, but I'm struggling to do it correctly.
Here's the provider I have:
(function() {
var app = angular.module('application.providers', ['application.services'])
app.provider('accessProvider', function() {
this.$get = ['userServices',
function(userServices) {
var auth = {}
auth.action = function(action, roles) {
auth[action] = angular.copy(roles)
}
auth.action('sell', [...])
auth.can = function(action) {
var userRole = userServices.getRole()
return auth[action].reduce(function(prev, now) {
return prev || (now === userRole)
}, false)
}
return auth
}
]
})
})()
When defining my routes here:
(function() {
var app = angular.module('application.configRoutes', ['ngRoute', 'application.providers'])
app.config(['$routeProvider', '$locationProvider', 'accessProvider',
function($routeProvider, $locationProvider, accessProvider) {
...
The error message I receive is:
Uncaught Error: [$injector:modulerr] Failed to instantiate module landingPage due to:
Error: [$injector:modulerr] Failed to instantiate module application.configRoutes due to:
Error: [$injector:unpr] Unknown provider: accessProvider
This is how I initialize my app:
var app = angular.module('landingPage', [
'application.configRoutes',
'application.directives',
'application.controllers',
'application.translate',
'application.flash',
'application.customInterceptors'
])
I load the file containing services.js
before providers.js
, and only then I load configRoutes.js
.