derekdreery
derekdreery

Reputation: 3912

Injecting Services from different modules in angular js

I have the following code

angular.module('go.core', ['go.auth'])

.controller('MainCtrl', ['$scope', 'AuthService', '$location',
function($scope, AuthService, $location) {
    //if(!AuthService.authorized) {
        $location.path('/login');
    //}
}])

.controller('LoginCtrl', ['$scope', '$location',
function($scope, $location) {

}]);

angular.module('go', [
    'ngRoute',
    'go.core'
])

.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);
    $routeProvider
        .when('/login', {
            templateUrl: 'partials/login.html',
            controller: 'LoginCtrl'
        })
        .when('/', {
            templateUrl: 'partials/main.html',
            controller: 'MainCtrl'
        })
        .otherwise({
            redirectTo: '/',
            controller: 'MainCtrl'
        });
    }
]);

angular.module('go.auth', [])

.service('AuthService', ['$scope', '$location',
function($scope, $location) {
    return {
        example: function() {console.log("here");}
    };
}]);

The MainCtrl controller does not inject the AuthService despite the fact that go.core requires go.auth. Can anyone help me with this?

Upvotes: 0

Views: 224

Answers (1)

tasseKATT
tasseKATT

Reputation: 38490

You should be getting the following error:

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- AuthService

This is because you are trying to inject $scope into AuthService, which you cannot do. You can inject $rootScope however.

Remove it and it should work.

Upvotes: 2

Related Questions