Search code examples
angularjsangular-servicesangular-controller

Should I have more angular services or controllers?


I am developing an angular web game where I have a bunch of quests that use a single html template for the view. The problem I have is how to organize the Angular code.

Should I have only one controller (which will hold the non-quest-specific logic) and uses a bunch of services (one for each quest-specific logic)? The controller would probably have a big if/else block to use the right service for the chosen quest.

Or should I have one controller for each quest? That way we can use fewer services overall and share them among the various controllers for the quests.

Which one makes more sense and would potentially have the fewest code duplication? Could there be a better alternative to both?


Solution

  • I just suggest you to go for a controller per unit and build the services thinking as elements that you can share between controllers structuring them as functional independent shareable elements.

    Quest 1 = controller 1 -> inject all services needed for that quest
    Quest 2 = controller 2 -> inject all services needed for this one
    .......