2014-05-18 108 views
0

我有以下代码注射服务

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");} 
    }; 
}]); 

MainCtrl控制器不尽管go.core要求go.auth事实注入AuthService。谁能帮我这个?

回答

2

您应该会收到以下错误:

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

这是因为你试图注入$scopeAuthService,你不能这样做。但是,您可以注入$rootScope

删除它,它应该工作。

+0

谢谢非常。我在控制器而不是服务中寻找问题。 – derekdreery

+0

不客气:) – tasseKATT