1

我有一个main module和一些submodules。我正在注入submodules. mainmodule现在我想访问`子模块的服务-我该怎么做?

var myApp = angular.module('myApp', [
    'AuthenticationModule',
]);

myApp.controller('TestController', function($scope, 'AuthenticationModule') {
        /* How do I use the service? */
        $scope.testVar = AuthenticationModule.SERVICE?????
});

submodule

var AuthenticationModule = angular.module('AuthenticationModule', []);

AuthenticationModule.service('TestService', function() {
    this.testFunction = function() {
        return 'You successfully got me!';
    };
});
4

1 回答 1

2

In angular, you do not inject modules. Instead, you declare the dependencies among modules and then all the controllers, services, directives and values from the dependencies are available in the application module.

How you would do it in your case is most likely this:

var AuthenticationModule = angular.module('AuthenticationModule', []);

AuthenticationModule.service('TestService', function() {
    this.testFunction = function() {
        return 'You successfully got me!';
    };
});

var myApp = angular.module('myApp', [
    'AuthenticationModule',
]);

myApp.controller('TestController', function($scope, 'TestService') {
        /* Injected the "TestService" directly */
        $scope.testVar = TestService.testFunction();
});
于 2013-11-12T20:27:12.973 回答