AngularJS 中的嵌套模块 [英] the Nested Modules in AngularJS

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

问题描述

我有 N 个角度模块.其中之一是引导并存在整个页面生命周期的根容器.所有其他 N-1 都是嵌套在页面内的游戏,需要动态加载和卸载(如何卸载 angularjs 模块).

I have N angular modules. One of them it is the root container that bootstrapped on and exists whole page life cycle. All other N-1 are games that nested inside of the page and need to be loaded dynamically and unloaded (How to unload angularjs modules) as well.

.------------------.
|Container         |
|  .----------.    |
|  |Game      |    |
|  |          |    |
|  |          |    |
|  `----------`    |
`------------------`

详情

每个游戏 - 都以相同的方式使用 angular-module 作为依赖主容器来进行一些 API 交互.

Details

Each game - is the same way angular-module that has as a dependency main container to do some API interaction.

经过一些实验,我发现在 Angularjs 1.1.5 嵌套模块中几乎(!) 工作正常:http://plnkr.co/edit/kJao7o

After some experiments I've found that in Angularjs 1.1.5 nested modules almost(!) works right: http://plnkr.co/edit/kJao7o

但是新的 Angularjs (1.2.0-rc2) 坏了:http://plnkr.co/edit/ZyhbRu

But with new one Angularjs (1.2.0-rc2) is broken: http://plnkr.co/edit/ZyhbRu

这个例子仍然引发异常:

This example still raises exception:

错误:参数GameCtrl"不是函数,未定义

Error: Argument 'GameCtrl' is not a function, got undefined

我尝试使用 ng-include 来防止来自 AngularJS 的自动注入.但仍然有同样的问题 (http://plnkr.co/edit/EM1MbB);

I've tried to use ng-include to prevent auto-injection from AngularJS. But still have same problem (http://plnkr.co/edit/EM1MbB);

我知道不可能在同一页面上使用嵌套的 ng - 应用程序http://docs.angularjs.org/api/ng.directive:ngApp

I know that it's impossible to use the nested ng - app on the same page http://docs.angularjs.org/api/ng.directive:ngApp

但是如何在同一页面上使用多个嵌套的 angular 模块(有自己的控制器)?

But how it is possible to use more than one nested angular modules on the same page (with its own controllers)?

而且我不知道这只是暂时的错误还是 Angular 团队决定避免使用嵌套模块.有人知道吗?

And I don't know is it just was temporary bug or Angular team decide to avoid of nested modules. Is there anybody know something about that?

或者,如果针对此类问题使用模块是个坏主意 - 对此的最佳决定是什么?

Or, if using modules for such issue is bad idea - what is the best decision for that?

  • 名称范围的隔离(以避免名称冲突).我可以保证未来两个或多个游戏中的任何一个都不会使用相同的 ctrls 名称之类的东西;
  • 要卸载模块(我不知道在 AngularJs 中是否可行 - 但我要尝试);

推荐答案

哦!我刚刚意识到你的想法.其实你对Angular构建组件的理解有误.

Oh! I just realized you idea. Actually you have incorrect understanding of Angular building components.

  1. Angular 模块只是控制器、指令、工厂等的逻辑包装器.例如,您将拥有模块main"和几个子模块moduleA"和moduleB"

它们与标记上的模块没有任何关系,它只是您应用中的一个逻辑单元.

They doesn't have any relation to the modules on a markup, it's just a logical unit in your app.

  1. 在您的示例中,您希望指的是范围.你的应用程序中可能有多个范围,它们可以被继承或有孩子.在这种情况下,您可以加载或卸载创建范围的实体
  1. In your example you hopefully mean scopes. You may have several scopes in your app and they can be inhireted or have children. And in this case you can load or unload entity that created the scope

查看官方开发者指南了解更多详情.

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

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