簡體   English   中英

從角度js中的不同模塊注入服務

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM