如何在angularjs中的多个js文件中拆分单个控制器 [英] How to split single controller in multiple js files in angularjs

查看:27
本文介绍了如何在angularjs中的多个js文件中拆分单个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个大型应用程序从 silver-light 重写为 angularjs,在执行此操作时,我意识到我的每个控制器 js 文件跨越 2000-3000 行代码.我所有的代码都严重依赖于 scope 变量.想知道是否有机会将单个控制器js文件拆分为多个js文件?任何指针将不胜感激.

I am rewriting a large application from silver-light to angularjs, while doing it, I am realizing that each of my controller js file is spanning 2000-3000 lines of code. All of my code is heavily dependent on scope variables. Wondering if there is any opportunity to split single controller js file into multiple js files? Any pointer would be appreciated.

推荐答案

使用多个控制器来处理页面中的任务.如果使用大型控制器是不可避免的,您可以通过将范围传递给另一个方法,然后在那里定义其余方法,将定义拆分为多个文件.

Use multiple controller to handle the task in your page. If using a larg controller is unavoidable ,you can split the definition to multiple files by passing the scope to another method and then define the rest of methods there.

在第一个文件中:

app.controller('CtrlA', function($scope){
app.expandControllerA($scope);

});

在第二个文件中

app.expandControllerA = function($scope)
{

}

您可以将任何变量或依赖项传递给 expandControllerA 函数.

You can pass any variable or dependencies to expandControllerA function.

这篇关于如何在angularjs中的多个js文件中拆分单个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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