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

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

问题描述

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

查看文档以获取更多信息。

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

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

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

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