问题描述
我有一个包含多个容器的页面.每个容器都有自己的控制器,但指向一个工厂,该工厂处理与 Web 服务 API 交互的所有逻辑.我想为每个控制器创建一个单独的文件但是我希望所有这些都包含在一个模块中.在我的一生中,我找不到如何将来自不同文件的控制器包含到一个模块中.
I have a page containing multiple containers. Each container will have its own controller but point to one factory, which handles all the logic interacting with a web service API. I would like to have a separate file for each controller BUT I want all of this inside of one module. for the life of me I cannot find how to include controllers from different files into one modules.
//file 1
MyController ....
//file 2
MyOtherController
//file 3
MyFactory
//file 4
The Module
该模块将由定义在三个单独文件中的 MyController、MyOtherController 和 MyFactory 组成.有人可以帮助解决这个问题或为我指出一个好的资源吗?谢谢!
The module would be composed of MyController, MyOtherController and MyFactory defined in three separate files. Can someone help with this or point me to a good resource? Thanks!
推荐答案
您可以将模块视为应用程序不同部分(控制器、服务、过滤器、指令等)的容器.要访问容器,只需调用例如它的模块名称
You can think of a module as a container for the different parts of your app – controllers, services, filters, directives, etc. To access a container just call its module name for example
//file.4
angular.module("theModule",[]);
既然您已经在 angular 中声明了主模块,现在您可以使用 angular 从任何地方访问 mainModule
now that you have declared main module within angular now you can access mainModule from anywhere using angular
//文件1
angular.module("theModule").controller("MyController",[function(){...}]);
//文件2
angular.module("theModule").controller("MyOtherController",[function(){...}]);
//文件3
angular.module("mainModule").factory("MyFactory",[function(){...}]);
这篇关于AngularJS 模块和外部控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!