AngularJS 模块和外部控制器 [英] AngularJS Modules and External Controllers

查看:26
本文介绍了AngularJS 模块和外部控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个容器的页面.每个容器都有自己的控制器,但指向一个工厂,该工厂处理与 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(){...}]);

查看文档了解更多信息.

我还建议阅读 Google 的样式指南和约定手册

另请阅读有关设置应用程序结构以实现可维护性的信息

这篇关于AngularJS 模块和外部控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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