Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App architecture: directives vs. controllers

Tags:

angularjs

I have a big app, which contains many layouts and subviews. Looks like (simplified): http://plnkr.co/edit/x4kleCCQH5Hsy6dcjgXe?p=preview

So, I have many directives and services. And only 2-4 simple controllers (10 - 20 lines of code). All logic stored into big directives, which contain many small directives. Am I wrong?

like image 865
Gm0t Avatar asked Jan 23 '13 12:01

Gm0t


1 Answers

The way I'm approaching it is.

  • Directives contain view logic, not business logic. This is also where DOM-messing about happens if needed.

  • Controllers are fairly thin, have minimal business logic.

  • Angular Services are where most of the heavy lifting is done.

If you have logic that needs to be re-used by multiple areas/controllers or is stateful - it's probably a better fit to put into a Service than a Directive.

Depending on your app/architecture - you could be posting to a server, and having the heavy BL happen on the server side.

The Angular docs have a nice bit on Using Directives Correctly that has a few pointers.

like image 79
e82 Avatar answered Oct 22 '22 21:10

e82