简体   繁体   English

一个AngularJS文件中有多少个控制器(服务,工厂,指令)

[英]How many controllers (services,factories,directives) in one AngularJS file

My AngularJS application is growing and we have over one hundred controllers, services and directives in one file. 我的AngularJS应用程序正在增长,我们在一个文件中有一百多个控制器,服务和指令。 Line count is approaching 25,000 and I'm wondering if I am approaching some limit (Angular, JavaScript or otherwise). 行数接近25,000,我想知道我是否正在接近某个限制(Angular,JavaScript或其他)。 Am I? 是吗

You should just do one thing (one controller, one service, one module) per file and structure your files into an organized folder structure, so that it is easy to find code (and to seperate scopes using IIFE s). 您只需为每个文件做一件事(一个控制器,一个服务,一个模块),然后将文件结构化为一个有组织的文件夹结构,这样就很容易找到代码(并使用IIFE分隔作用域)。 I recommend to read a style guide or an article on the topic . 我建议阅读有关该主题 的样式指南文章

The total amount of controllers, services etc. that can exist in your application are only limited by your RAM. 应用程序中可以存在的控制器,服务等的总量仅受RAM限制。 Performance should not be influenced, as searching for controllers/services to inject is O(n) if not better(its just a simple lookup that happens once per injection/instantiation). 性能不应受到影响,因为搜索要注入的控制器/服务的效果最好是O(n)(它只是每次注入/实例一次的简单查找)。

There probably isn't really a line limit, but I can imagine your file being insanely chaotic and almost unreadable. 可能实际上没有行限制,但是我可以想象您的文件疯狂混乱并且几乎无法读取。 You should consider to split your 1 file in a couple of files, like 1 file for your controllers (or if you have clear groupings of controllers, separate the controllers in a few files aswell) and 1 file for your services etc. You can create new modules for the services and controllers and then inject those in your main module. 您应该考虑将您的1个文件拆分为几个文件,例如用于控制器的1个文件(或者,如果控制器有清晰的分组,则将控制器也分成几个文件)和用于服务的1个文件等。您可以创建服务和控制器的新模块,然后将它们注入到您的主模块中。

The huge advantage: your code is much easier to maintain and understand, and it is much easier to track down errors and bugs instead of having to scroll through 25k lines of code. 巨大的优势:您的代码更易于维护和理解,并且更容易查找错误和错误,而不必滚动浏览25k行代码。

Also, should you for some reason pass the project on to someone else, he/she will be able to read your project and actually be able to adjust/expand it without spending weeks trying to figure the entire thing out. 另外,如果您出于某种原因将项目传递给其他人,他/她将能够阅读您的项目并实际上能够进行调整/扩展,而无需花费数周的时间试图弄清整个问题。

imagine some1 getting acces to your file and replace all your ';' 假设某人访问了您的文件并替换了所有的“;” with the greek questionmark, which looks pretty much identical. 与希腊问号,看起来几乎一样。 Good luck figuring that out if all your code is in 1 file! 祝您好运,如果您所有的代码都在一个文件中!

25,000 lines with over 100 controllers/services/directives in a single file can't be good. 在单个文件中包含2 5,000行,超过100个控制器/服务/指令是不好的。 Consider breaking up your application into various folders (controllers, services, directives, views, etc.) and try and keep as little code per file as possible. 考虑将您的应用程序分解为多个文件夹(控制器,服务,指令,视图等),并尝试使每个文件的代码尽可能少。 Ideally, 1 service/controller per file when in development! 理想情况下,在开发中每个文件1个服务/控制器! How do you even deal with that much code in a single file when developing? 开发时,您甚至如何在单个文件中处理那么多代码? You can concat and minify everything with Gulp/Grunt when linking to your index/html file. 链接到index / html文件时,您可以使用Gulp / Grunt合并并缩小所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM