【问题标题】:inject controller from different file using same module使用相同模块从不同文件注入控制器
【发布时间】:2015-09-10 03:09:21
【问题描述】:

我有两个文件

test1.js

angular.module("test1",[]).controller("test1Ctrl",function(){
// some code
})

test2.js

angular.module("test2",[]).controller("test2Ctrl",function(){
//some code
})

app.js

var app = angular.module("testApp",['test1','test2']);

我的问题是如何使用相同的模块而不使用模块 test1 和 test2 从 test1.jstest2.js 将我的两个控制器注入 app.js?

【问题讨论】:

  • 您问题中的代码有什么问题?在您的应用程序中使用独立模块没有任何问题。事实上,我强烈推荐它
  • @Phil 谢谢。但是有什么办法吗?
  • 你的意思是:使用来自不同应用程序的控制器?如果是的话——你总是可以在 HTML 中同时使用 2 个 ng-apps。你也可以在 2 个不同的地方使用同一个 ng-app
  • 您也可以寻找 ng-directive。正如所说的控制器是特殊指令。

标签: angularjs dependency-injection module controller


【解决方案1】:

如果您想将控制器保存在单独的文件中,

app.js

var app = angular.module("testApp",[]);

test1.js

app.controller("test1Ctrl",function(){
// some code
})

test2.js

app.controller("test2Ctrl",function(){
// some code
})

终于在html中包含js文件,

<script src="app.js"></script>
<script src="test1.js"></script>
<script src="test2.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2016-10-17
    • 2013-09-18
    相关资源
    最近更新 更多