AngularJS 模块和外部 Controller

标签 angularjs angularjs-controller angularjs-factory angularjs-module

我有一个包含多个容器的页面。每个容器都有自己的 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/

相关文章:

angularjs - UI Bootstrap 预输入 : Make input invalid if no option is selected

自动完成所需的 AngularJS 多个范围

javascript - 将名称参数添加到范围时发生 AngularJS 测试错误?

AngularJS - 如何避免使用 $timeout 等待创建元素?

javascript - (AngularJS) 无法将工厂注入(inject) Controller

javascript - 在动态生成的表angularjs中显示/隐藏元素(div)

AngularJS (ng-grid) "editableCellTemplate"在失去焦点时仍处于编辑模式

javascript - 以编程方式插入 HTML 时,无法为 trix-editor 中的任何标签添加 Id 或 class 等属性

angularjs - 将数据返回到 Controller 时,AngularJS Factory 出现问题

javascript - 在服务/工厂中使用 $http.get 返回一个集合