有条件注入模块AngularJS应用 [英] Inject modules conditionally in AngularJS app

查看:201
本文介绍了有条件注入模块AngularJS应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.)

这篇关于有条件注入模块AngularJS应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆