问题描述
我有一个包含多个容器的页面。每个容器都有自己的控制器,但指向一个工厂,该工厂处理与Web服务API交互的所有逻辑。我想为每个控制器 BUT 提供一个单独的文件,我希望所有这些文件都在一个模块中。在我的生命中,我找不到如何将不同文件中的控制器包含到一个模块中。
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
//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模块和外部控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!