[英]Injecting Services from different modules in angular js
我有以下代碼
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
控制器不注入AuthService
盡管事實上go.core
需要go.auth
。 誰能幫我這個?
您應該收到以下錯誤:
Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- AuthService
這是因為您試圖將$scope
注入AuthService
,這是您無法做到的。 但是你可以注入$rootScope
。
刪除它,它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.