如何在多个文件中定义控制器 - AngularJs [英] How to define controllers in multiple files - AngularJs
问题描述
我试图定义在不同的文件的控制器,但我得到的错误:
transactionsController不是一个函数得到未定义
文件结构
我已经在这个序列中添加的文件
1- common.js
2- transactions.js
Common.js
在共同文件,我已经定义
VAR应用= angular.module(spModule,[ngMessages,ui.bootstrap]);
Transactions.js
angular.module('spModule')。控制器('transactionsController',
['$范围,$ HTTP',函数($范围,$ HTTP){}]
);
HTML文件
<机身NG-应用=spModuleNG控制器=transactionsController>
首先,你应该摆脱全球应用
的变量。这是没有必要的。其次,你必须了解 angular.module()
方法的原理。
使用 angular.module()
有两个参数(如 angular.module(我的模块,[])
)将导致的设置新与其相应的依赖模块。相比之下,使用 angular.module(我的模块)时
相应的模块内部抬起头来,返回给调用者(获得)
当你第一次定义您的应用程序,你可能只是创建下列文件和结构的手段。
app.js
angular.module('对myApp',[]);
FirstController.js
angular.module('对myApp')控制器('FirstController',函数(){})。
SecondController.js
angular.module('对myApp')控制器('SecondController',函数(){})。
如果您现在还包括在这个特别为了您的HTML文档中的这些文件(至少 app.js 必须是第一位的),您的应用程序的工作只是在两个单独的文件两个独立的控制器罚款。
进一步阅读
我可以推荐 AngularJS风格指南模块上中获取关于这一主题更多的想法。
I am trying to define controllers in separate files, but I'm getting the error:
transactionsController not a function got undefined
File structure
I have added files in this sequence 1- common.js 2- transactions.js
Common.js In common files I have defined
var app = angular.module("spModule", ["ngMessages", "ui.bootstrap"]);
Transactions.js
angular.module('spModule').controller('transactionsController',
['$scope', '$http', function ($scope, $http) {} ]
);
HTML FIle
<body ng-app="spModule" ng-controller="transactionsController">
First, you should get rid of the global app
variable. This is not necessary. Second, you have to understand the principle of the angular.module()
method.
Using angular.module()
with two parameters (e.g. angular.module('my-module', [])
) would result in setting a new module with its corresponding dependencies. In contrast, when using angular.module('my-module')
the corresponding module is looked up internally and returned to the caller (getting).
The means when you first define you application you might just create the following files and structure.
app.js
angular.module('myApp', []);
FirstController.js
angular.module('myApp').controller('FirstController', function () {});
SecondController.js
angular.module('myApp').controller('SecondController', function () {});
If you now include these files in your html document in this particularly order (at least app.js has to come first), your application works just fine with two separate controllers in two separate files.
Further readings
I can recommend the AngularJS Styleguide on modules for getting more ideas on this topic.
这篇关于如何在多个文件中定义控制器 - AngularJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!