有条件注入模块AngularJS应用有条件、模块、AngularJS

2023-09-13 04:40:09 作者:你情深似海却不曾为我而蓝

我的角的应用程序的结构是这样的:

My Angular app structure is like this:

App.js

angular.module('RateRequestApp', [
    'RateRequestApp.services',
    'RateRequestApp.controllers',
    'ui.bootstrap',
    'angular-loading-bar',
    'textAngular',
    'angularFileUpload'
]);

我使用不同的页面不同的HTML文件,我不使用角的 $路线,但我仍然想在不同的控制器上的所有页面中使用相同的应用程序。

I am using different HTML files for different pages and I am not using Angular's $route, but still I want to use same app in all pages with different controllers.

正如你可以看到我注入第三方模块到我的应用程序。问题是,在一些网页我不希望一些这方面的模块,我怎样才能避免他们在哪里我也不需要他们?

As you can see I am injecting third party modules to my app. The problem is that in some pages I don't want some of this modules, How can I avoid them where I don't need them?

推荐答案

是的,你可以做到这一点是这样的:

Yes you can do it something like this:

var myApp = angular.module('RateRequestApp', [
    'RateRequestApp.services',
    'RateRequestApp.controllers',
    'ui.bootstrap',
    'angular-loading-bar'
]);

var lazyModules = ['textAngular', 'angularFileUpload'];

angular.forEach(lazyModules, function(dependency) {
    myApp.requires.push(dependency);
});

在此情况下,您可以有条件地注入模块。 (但请注意,某些模块的延迟加载可能无法正常工作哪里有需要进行一些配置。)

In this case you may inject the modules conditionally. (But please note, lazy loading of some module may not work where there is some configuration needed.)