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

angularでは、注入しませんmodules。代わりに、モジュール間の依存関係を宣言すると、依存関係からのすべてのcontrollersservicesdirectivesおよびvaluesがアプリケーション モジュールで使用可能になります。

あなたのケースでそれを行う方法は、おそらく次のとおりです。

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 に答える