AngularJS:动态地在运行时定义模块依赖

AngularJS: Defining module dependency at run-time, dynamically

本文关键字:定义 模块 依赖 运行时 动态 AngularJS      更新时间:2023-09-26

假设我有一个简单的模块:

角。模块("平台",[]);

假设我有另一个模块:

角。模块("PlatformProductA",[]);

是否有一个我可以从JavaScript调用的Angular函数,它将把模块'PlatformProductA'作为一个依赖注入到模块'Platform'中,就像模块'Platform'已经定义为这样:

角。模块("平台",[' PlatformProductA ']);

换句话说,在定义了一个模块之后,我需要将另一个模块作为依赖项注入到它中。

编辑:

我不知道如何,但这似乎工作:

angular.module('Platform', []);
angular.module('PlatformProductA', []);
angular.module('Platform').requires.push('PlatformProductA');

Angular不知怎么地看到'PlatformProductA'被注入了?

是的,有$injector,它允许动态注入

angular.module('Platform', [$injector], function(){ // don't remember exact syntax
    $scope.myModule = $injector.get('PlatformProductA');
});