简体   繁体   English

SPA中按需加载控制器-AngularJS

[英]Load controllers on demand in SPA - AngularJS

I am new to the whole SPA thing, but i interested of develop one. 我是整个SPA领域的新手,但是我有兴趣开发一个。 All the tutorials on the web has 2 controllers that are included inside the main page. Web上的所有教程都有2个控制器,它们都包含在主页中。 My question is what is the best practice to load the controllers on demand, for example, if i have 5 pages (called partials in SPA world), each one of them has different logic. 我的问题是按需加载控制器的最佳实践是什么,例如,如果我有5页(在SPA世界中称为“局部”),则每个页面都有不同的逻辑。 so i will have 5 controllers, one for each page. 所以我将有5个控制器,每个页面一个。 I don't think that load all the 5 controllers in the main page is the good method, so how can i load angularJS controller on demand? 我认为加载主页中的所有5个控制器不是一个好方法,因此如何按需加载angularJS控制器? and how can i unload the controller that was used in the previous "pages"? 以及如何卸载先前“页面”中使用的控制器?

if someone has a good tutorial for spa with angular that has more than 2-3 controllers, i will be happy for a link. 如果有人有一个不错的水疗教程,其中包含2-3个以上的控制器,我将很高兴获得链接。

我认为这篇博客文章回答了我的问题, AngularJS + RequireJS

We have achieved that using ocLazyLoader. 我们已经使用ocLazyLoader实现了这一目标。 It is dependent states, and for each state you will have to specify the js files to be loaded. 它是依赖状态,对于每个状态,您将必须指定要加载的js文件。 Here is the link https://oclazyload.readme.io/ 这是链接https://oclazyload.readme.io/

Although, I would suggest to use minification of the files using grunt or other tool. 虽然,我建议使用grunt或其他工具来缩小文件大小。 This would help in increasing the performance as well because network data transfer is not an issue anymore, but to open a connection to server is time consuming. 这也将有助于提高性能,因为网络数据传输不再是问题,但是打开与服务器的连接非常耗时。 So, its better to minify and load everything at once. 因此,最好同时缩小和加载所有内容。

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

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