Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I started working with AngularJs recently.

Looking to understand the reason for using multiple controller, I found different site explaning how to use multiple controller (AngularJS site). But what I'm actually looking for is a rationnal for using multiple controller.

So my question is : Why or when should we use multiple controller in a project? and the subquestion that is tied to this question: is it a good pratice to use multiple controller in an Angular project.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

That's also an MVC question as angular extends this pattern. In Apple's View Controller Programming Guide for iOS, it says :

Every view is controlled by only one view controller.

So the idea in MVC pattern is to separate views. By having 1 Controller per View it makes it easier to achieve this. it simplifies the organization of controllers that serve one module. You do not suffer from code smells.

Also, it is important for routing issues in app.js for angular case. It clarifies structure for other developers that will have look at project. Using testacular in angularjs, unit testing is great, having multiple controllers makes unit testing easier.

Edit :

You would also most likely need more controllers for further functionalities. For example a Auth Controller where users can create new accounts. In addition to this you need a superadmin view where you can edit the resources with higher privileges. In such a case it is quite common to have separate controller. Scope and security issues has to change.

share|improve this answer
    
So if I understand your Edit ... Controller could be link to specific functionnality as well as specific view? –  David Laberge Jul 1 at 13:16
    
Exactly, this depends on your business logic also. In some though cases, they also have one controller for every database table, but that's really a though case for specific business needs. –  Yagiz Ozturk Jul 1 at 13:21
add comment

It is just a very good pratice to use 1 controller per 1 view. So for example you should have seperate controller for /home view, another one for /gallery, and another /contact.

It forces you as a developer to organise your code, so that you can take advantage of using services, filters etc.

Also it is easier to write unit tests because you can see what is covered and what is not.

share|improve this answer
    
Do you have any references for that approach? –  David Laberge Jul 1 at 12:53
    
What do you mean ? –  Michał Lach Jul 1 at 12:58
    
I like your explanation, but I would like to read more about it, explore that approach? Where should I go? Or where did you get that from? Thanks –  David Laberge Jul 1 at 13:00
    
I work in team with another front-end devlopers on 2 projects that use Angular as main UI framework. We have quickly discovered that as projects grew they quickly became more clutteted, and harder to organise. We were also getting a lot merge conflicts in Git. It was also unclear who is responsible for writing unit tests for what. With the aproched described above, we managed to work out most of this problems. –  Michał Lach Jul 1 at 13:08
    
Thanks very helpful, we are running on the same pattern here where I work. –  David Laberge Jul 1 at 13:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.