Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: How do I create controllers in multiple files

Tags:

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'); 
like image 649
user1703761 Avatar asked Sep 29 '12 19:09

user1703761


People also ask

Can we have multiple controllers in AngularJS?

An AngularJS application can contain one or more controllers as needed, in real application a good approach is to create a new controller for every significant view within the application.

Can you create controller in AngularJS?

AngularJS ControllersAngularJS applications are controlled by controllers. The ng-controller directive defines the application controller. A controller is a JavaScript Object, created by a standard JavaScript object constructor.

Can we have multiple Ng-controller?

If you really want to have two ng-controllers across a page you can do by separating divs. I have add some of my app. js codings in to the above post. I have two .

Can we have multiple modules in AngularJS?

Yes, you can define multiple modules in angularJS as given below. The modularization in AngularJS helps us to keep the code clarity and easy to understand, as we can combine multiple modules to generate the application.


1 Answers

here is what I did:

index.html

<script src="js/angular.js" type="text/javascript" charset="utf-8"></script> <script src="js/myApp.js" type="text/javascript" charset="utf-8"></script> <script src="js/myCtrlA.js" type="text/javascript" charset="utf-8"></script> <script src="js/myCtrlB.js" type="text/javascript" charset="utf-8"></script> 

app.js

myApp = angular.module('myApp', []) myApp.config ($routeProvider) ->     $routeProvider.when('/a', {controller: 'myCtrlA', templateUrl: 'a.html'})     $routeProvider.when('/b', {controller: 'myCtrlB', templateUrl: 'b.html'}) 

myCtrlA.js

angular.module('myApp').controller 'myCtrlA', ($scope) ->     console.log 'this is myCtrlA' 

myCtrlB.js

angular.module('myApp').controller 'myCtrlB', ($scope) ->     console.log 'this is myCtrlB' 

as you can see, if I have a lot of controller js files, that will be a lot of script elements in index.html too.
I don't know how to deal with that yet.

FYI: http://briantford.com/blog/huuuuuge-angular-apps.html
but this article did not mention the html file too.

like image 109
zx1986 Avatar answered Oct 02 '22 13:10

zx1986