我有一个包含多个容器的页面。每个容器都有自己的 Controller ,但指向一个工厂,该工厂处理与 Web 服务 API 交互的所有逻辑。我希望每个 Controller 都有一个单独的文件但是我希望所有这些都在一个模块中。在我的一生中,我找不到如何将来自不同文件的 Controller 包含到一个模块中。
//file 1
MyController ....
//file 2
MyOtherController
//file 3
MyFactory
//file 4
The Module
该模块将由定义在三个单独文件中的 MyController、MyOtherController 和 MyFactory 组成。有人可以帮忙解决这个问题或给我指出一个好的资源吗?谢谢!
最佳答案
您可以将模块视为应用程序不同部分的容器—— Controller 、服务、过滤器、指令等。要访问容器,只需调用其模块名称即可
//文件.4
angular.module("theModule",[]);
现在您已经在 angular 中声明了主模块,现在您可以使用 angular 从任何地方访问 mainModule
//文件1
angular.module("theModule").controller("MyController",[function(){...}]);
//文件2
angular.module("theModule").controller("MyOtherController",[function(){...}]);
//文件3
angular.module("mainModule").factory("MyFactory",[function(){...}]);
Check out the documentation for more information.
I also suggest reading Google's style guide and conventions manual
Also read about setting up app structure for maintainability
关于AngularJS 模块和外部 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25518419/