3

I'm looking for best practices on services, controllers and directives for Angularjs 1.3.X.

Here is the rules i noticed :
- The view (html) knows only the main controller and his scope
- A directive should not have his own services

So :
- The view (html) cannot call the services directly.
- The view (html) must use the main controller to access the services.
- The view must declare the binding between the directive and the main controller

And then :
In the code, if a directive have to call a business function F1 of services :
- the main controller must publish a function F2 which wrap the function F1
- the view must declare F2 in the directive configuration

You can look at the picture attached for a more visual explaination.

Questions :
- Are you agree with those rules and the style of code ?
- Do you have any additional rules ?
- Why should we not follow these rules ?

enter image description here

5
  • 1
    How about directive -> service? Commented Nov 20, 2014 at 16:09
  • 2
    Small controller and a lot of service seems to be a good practice to me. Commented Nov 20, 2014 at 16:13
  • For me, Business Services must be access through the main controller. Commented Nov 20, 2014 at 16:28
  • Directive is basically component. I have no problem with calling service in directive. It can be independent on controller. Commented Aug 8, 2015 at 11:32
  • Your architecture will cause the root controller to be too complex on large applications, because it has to store all links to services used in application. I prefer split code into components. Each of components has its own controller and that controller calls some services where needed. This provides me clean logic separation. Commented Aug 24, 2015 at 6:55

1 Answer 1

1

those are most popular styling guides and well discussed in my opinion (all three are collaborating):

https://github.com/johnpapa/angular-styleguide

https://github.com/jmcunningham/angularjs-styleguide

https://github.com/toddmotto/angularjs-styleguide

short answer: your approach seems very similar to those. you might want to take a closer look to them

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.