For a GameJam I'm trying to code a simple web-based text adventure game with sprites and small animations in AngularJS. I'm a little new to Angular and am having some trouble figuring out how to structure it. I think my trouble comes from my struggle with Controllers vs Directives.

My game essentially needs to pull text from a text file and display it within a DOM element when clicked. Accompanying that is a separate DOM item that displays a character sprite image. When certain bits of text are displayed, the image needs to animate.

Can anyone who knows AngularJS well walk me through the most clean and efficient way to structure this between my controllers, directives, and the HTML view?


I think you should use directives for the DOM elements and controllers for interaction between these directives. DOM manipulation should only be done inside a directive.

Fetching a text file could be done inside the controller, but it would be better if you did that through a service. Services can be used for reusable business logic/fetching data.

You could see it as the MVC pattern. Model/business-logic can be put in a service, View is composed of normal HTML elements and directives and the Controller who orchestrates communication between the two.

| improve this answer | |

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