wikiApp.config(['$routeProvider','authService',
function($routeProvider,authService) {
var admin = authService.getLoggedin();
$routeProvider
.when('/hjem',{
templateUrl: 'partials/homeContent.html',
admin: false
})
.when('/articles/:article',{
templateUrl: 'partials/articles.html',
admin: false
})
.when('/newArticle',{
templateUrl: 'partials/postArticle.html',
controller: 'articleController',
admin: true
})
The authService.getLoggedin() returns either false or true depending on if the user is logged in or not. Then i would like to not allow them to the Url if they are not allowed.
But i get this error: Error: [$injector:modulerr] Failed to instantiate module wikiApp due to: [$injector:unpr] Unknown provider: authService
So to inject a service in config you just need to call the Provider of the service by adding 'Provider' to it's name.
During the configuration phase you can only ask for providers ($routeProvider, $locationProvider etc.) it means you cannot inject any other instance, so I would suggest injecting your service in the run phase, there your will have an instance of your service.
If you want to call an external function (in your case Service function) form your routes (.config) as shown below: templateProvider.getTemplate('about')
you cannot create a Service or Factory for that. Instead you must create a Provider.
Here’s a real example of a Provider that generates the template path from the name:
The usage of such Provider in the routes (.config) will be as follow:
VIP Note:
to inject the provider you must postfix it with xxxProvider (that name of the provider should not be postfixed, only on injection in the .config).