If I understood your question well (and if my experience can answer well) I would say that you are not doing it wrong. In Angular, things get a little "scattered", the big question is to know how to "organize the mess".
Each of these (and there are many) elements have very specific purposes, which are often completely ignored. Whether for poorly crafted tutorials, for the convenience of writing everything in one place, generating less 'work' or even lack of knowledge.
Note: In Angular2 this already changes a lot, since it will be much simplified in this sense.
I, in particular, develop everything using well say only 3 components of the Angular:
- Controller
- Factory
- Directive
Each has a very simple but very distinct purpose, see:
Factory : Responsible for getting data from a backend
/ api
to feed the application with the required information.
Controller : Responsible for passing data (obtained by factory
) to the view and also for executing user-invoked functions through the UI.
Directive : Handling conditional HTML and / or creating common patterns functions in several views - Even though the views between them have no relation whatsoever.
In a simple scenario, imagine that you have an E-commerce. Once we access, we need to feed the site with the products. For this, the factory
calls the backend
and gets the data, storing it in a variable even within the factory
itself.
The controller
, in turn, will get this information in factory
- Not caring how it was obtained, just interested to know that it exists, then passing it to the view via $scope
, depending on the vm
you use), so we feed the site with the products.
Assuming you want to add a product to the shopping cart, but knowing that the product can be accessed on both the home page and the "Bestseller" page on the "My list" page, categories, etc. Note which is the same function being called from different locations. Therefore, we can apply the function to a syntax
, so it can be called independent of its directive
, view
or other component. The cart itself can be managed by a controller
also, and it is accessed from any directive
.
Although "messy", Angular has components with well-defined tasks and goals that can keep your application lean, unrepeatable, and in a way, well-organized. I believe it should be seen more as a whole than as isolated parts.
Complementary: A material that I have used ENOUGH and that has given me a good idea in organizing my code and my components, was this guide: link