问题描述
我有一个复杂的布局,用户可以把(拖/放)小部件的应用程序(通过从$ P $选择pdefined设置的100多个小部件),每一个部件是一个显示一组数据的自定义实现以特定的方式(使用REST调用获取)。我读过吨的博客文章,计算器问题和官方AngularJS文档,但我想不出我应该怎么设计我的应用程序来处理有要求。望着演示应用程序,有一个单独的模块(NG-APP),当构建它的js文件的依赖模块被声明为它的依赖,但是我有一个大组小部件,并不知它是不可取来形容他们所有那里。我需要为suggession以下问题:
- 我应该如何设计我的应用程序和小工具? - 我应该有一个单独的AngularJS模块或每个插件应该是一个指令,主模块
- 如果我设计我的小部件的指令,是有办法的指示中定义的依赖。即说,我的指令使用纳克压光机在其实施?
- 如果我设计每个插件作为一个单独的模块,有没有办法来动态的部件模块作为一个依赖添加到主模块?
- 我应该如何设计控制器 - 每个部件一个控制器可能
- 我应该如何分开状态(范围内),如果我从视图中的相同类型的多个小部件?
- 是否有bestpractices与AngularJS设计可重复使用的小部件?
修改
有用的参考资料:
- 之后
- AngularJS资源延迟加载href=\"https://github.com/nikospara/angular-require-lazy\">示范项目
- code组织大型AngularJS和JavaScript应用程序
这些只是一般的建议。
You're talking hundres of widgets, it seems natural to split them into several modules. Some widgets might have more in common than other widgets. Some might be very general and fit in other projects, others are more specific.
Dependencies to other modules are done on a module level, but there is no problem if module A
depends on module B
and both A
and B
depends on module C
. Directives are a natural choice for creating widgets in Angular. If a directive depends on another directive you either define them in the same module, or create the dependency on a modular level.
I'm not sure why you would want to do this, and I'm not sure how to do it. Directives and services are not initialized before they get used in Angular. If you have a huge library of directives (widgets) and know that you'll probably use some of them, but not all of them - but you don't know which ones will get used when the application gets initialized you can actually "lazy load" your directives after your module has been loaded. I've created an example here
The benefit is that you can get your application to load fast even if you have lots of code, because you don't have to load the scripts before you need them. The disadvantage is that there can be a considerably delay the first time a new directive is loaded.
A widget will probably need its own controller. Controllers should generally be small, if they get big you can consider if there's any functionality that would fit better in a service.
Widgets that need scope variables should without doubt have their own isolated scopes (scope:{ ... }
in the directive configuration).
Isolate the scope, keep the dependencies to a necessary minimum.See Misko's video about best practices in Angular
Brian Ford has also written an article about writing a huge application in Angular
这篇关于发展与动态设置模块的AngularJS应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!