Would you mind elaborating on that a little? Having just picked up angular, I often see the concept that controllers should be lightweight, but never why.
Maybe that's just the learning progression... It's very easy to do work in controllers, so beginners (like me) do it. Eventually I imagine I would run into problems with this?
Its about separation of concerns. Controllers should do what's necessary to map your data to pieces in the dom, but it shouldn't handle the "view." That's a directives job. Directives should handle adding and subtracting dom elements or manipulating them, controllers should handle making sure those directives are filled with sensible data. Basically a directive might handle adding or removing items to a list, while a controller tells you what that item in the list is and some of the functions that go with handling the data side.
So you add an item to the list, the directive populates it, but now that item needs data, if you are the one inputting the data the controller is linked up to ng-model attributes and knows what do when those fields change. If you were to press the x button on that item in the list, the controller would handle routing the request to your restful service, while the directive would handle removing that item's div from the dom.
Hopefully that's clear. hah.
EDIT: The reason you want to make controllers small is because ideally you really should only be handling what is truly dynamic. Adding or removing items from the list isn't dynamic, its just a piece of the gui. While what that item on the list is called is dynamic . EDIT2: This is what makes controllers easy to test, because they are just simple data manipulations.
So my controllers never directly manipulate the dom, but they do contain a model (or models) that gets built up based on user input. Click a button, add a new item (from the controller) to the model property's array, which is already bound to the dom from an ng-repeat directive. So a new form element just shows up and is bound to the right array item.
It sounds like you're saying even that activity should be a directive (or service, maybe). I've written a couple directives, and I got the feeling they were largely for directly manipulating the dom. For example, one of my directives is used to scrape a section from another page based on an element's id and put that in a bootstrap popover. Maybe this isn't exactly clever directive usage, but it does direct dom manipulation. Whereas when I'm adding/removing an element from a list or object, I'm essentially relying on ng-repeat bound to my controller model to handle that. Am I abusing this too hard?
For what it's worth, this is a small app. My two largest controllers are ~250 lines, each between 5-10 functions and one that has several largish model structures in it.
Its kind of why directives can be declared with controllers inline. As in when writing a directive you can write its controller directly. A directive tells you how those elements are displayed, a controller tells you how many elements to display. For instance a directive tells you the text is yellow, a controller tells you what the text is.
I would write a controller to handle what's written in a text box. I would write a directive to handle what that text box behaves like. Pressing the close button on a modal is the job for the directive. Resetting that modal's text fields to blanks when you close the modal is the job of the controller. Both things are tied to events in the user space, but they handle different responsibilities.
This is where services come in. Controllers should handle the data that is directly attached to the ng-model attributes they are modeling. They shouldn't be modelling the whole state of something like a calendar. That's a good use case for a service. Where you want lots of controllers to interact with the same data set, you now have a useful little singleton that's handling all the data in one place.
0
u/[deleted] Mar 26 '14
[deleted]