AngularJS:如何创建多个文件控制器 [英] AngularJS: How do I create controllers in multiple files
问题描述
我想我的控制器分割成多个文件,但是当我尝试在我的模块IM注册它们得到一个错误:
groupcontroller.coffee
应用程序= angular.module(网谈,[]);
app.controllerGroupController',($范围) - GT;
usercontroller.coffee
应用程序= angular.module(网谈,[]);
app.controllerUserController的',($范围) - GT;
错误
错误:参数'GroupController不是一个函数,得到了不确定
从我真的不得到什么模块方法不反正文档。是否储存我的控制器的关键网上聊天?
编辑:
这也似乎传递[]创建一个新的模块,并覆盖了previous有一个
应用程序= angular.module(网谈,[]);
要prevent这一点,你必须离开了[]像
应用程序= angular.module('在线访谈');
检查您的code其他地方,你会引用GroupController(可能在你的路线)。机会是你拥有了它,因为作为一个变量,但是当你声明一个模块中的控制器,你必须把它包报价。 EG:
MyCtrl1()= - > ()
...
$ routeProvider.when('/厂景',{templateUrl:'谐音/ partial1.html',控制器:MyCtrl1})
工作正常,因为MyCtrl1是一个变量。但是,在一个模块中声明控制器时,你是:
应用程序= angular.module(网谈,[]);
app.controllerGroupController',($范围) - GT;
#...$ routeProvider.when('/厂景',{templateUrl:'谐音/ partial1.html',控制器:'GroupController'})
GroupController'需要在航线的报价。
I'm trying to split my controllers into multiple files, but when i try to register them at my module im getting an error:
groupcontroller.coffee
app = angular.module('WebChat', []);
app.controller 'GroupController', ($scope) ->
usercontroller.coffee
app = angular.module('WebChat', []);
app.controller 'UserController', ($scope) ->
Error
Error: Argument 'GroupController' is not a function, got undefined
From the documentation I dont really get what the module method does anyway. Does it store my controller with the key 'Webchat'?
Edit: It also seems that passing [] creates a new module and overwrites the previous one
app = angular.module('WebChat', []);
To prevent this, you have to leave out the [] like
app = angular.module('WebChat');
Check other places in your code where you're referencing 'GroupController' (probably in your route). Chances are you have it as there as a variable, but when you declare a controller inside a module you'll have to wrap it quotes. EG:
MyCtrl1() = -> ()
...
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: MyCtrl1})
works fine because MyCtrl1 is a variable. But when declaring controllers in a module as you are:
app = angular.module('WebChat', []);
app.controller 'GroupController', ($scope) ->
# ...
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'GroupController'})
'GroupController' needs quotes in the route.
这篇关于AngularJS:如何创建多个文件控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!