0

I'm teaching myself to program with AngularJS.

As a learning exercise, I'm attempting to rewrite an application I've written using jQuery to use Angular instead.

The program draws fractal images on a canvas element. The user inputs data into forms describing what fractal to draw, and then clicks a button. When the user clicks the button, the program draws the fractal.

It seemed rather straightforward to me to create an Angular controller to bind the data on the form.

Next, I asked myself how I can use Angular to do the actual drawing. In a book I'm reading, it says to use a service for "business logic" that "drives the application", rather than presentation logic.

That's fine and good, but... my existing code already provides an API for drawing the fractals. It seems to me that all I need do is bind the [Draw] button that the user clicks after filling the forms so that it calls my existing code. My book seems to suggest that I should create a "Drawing Service" and inject it into my controller; that the controller should then call a method on the service and pass in the configuration from the forms.

That sounds fine to me, but I'm asking myself... why do this? Why not simply have my Angular code call my drawing function the way it is now instead of putting it in a service?

I think that there is some aspect of Angular that I have failed to grasp, so I'm hoping to understand why I should use a service.

Just to be clear, I am not asking about the different between .service and .factory

2
  • @825K, I specifically said that I'm not asking about service vs factory. If my question is not clear, please let me know. I'm asking why I should use a service at all. Commented May 28, 2015 at 21:40
  • Easier to reuse services (just inject into a different controller). Also better encapsulates logic from HTML interactions. Commented May 28, 2015 at 21:45

3 Answers 3

1

You are never really required to use a service. However, in larger scale applications, separating logic between a controller and a service allows you to separate (decouple) the logic of the application.

Services are most useful in cases where you re-use the same logic in multiple different areas of your application, or where the service implementation is complex enough to warrant independent testing or isolation. It is much easier to test a service with 10 lines of code than to try to test if those 10 lines of code worked inside a controller mixed with other functions.

If you are working in a multiple developer environment, having a service will allow someone who is an expert on the UI work on the UI independently from someone who knows everything there is to know about how this specific function operates.

Only you can determine when having a service is absolutely necessary, but most Agile developers will favor services whenever possible, and only use inline functions in a controller for trivial processes.

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

Comments

0

If you use a service instead of just using your api, you create an injectable version of that API. This is really useful for unit testing. I use lodash and sometimes TweenLite in my applications and I always wrap them in a service and inject them into my controllers, that way when I write the unit tests it is easier to mock them and doesn't create "untestable" code. Having an injectable service also makes it known that your controller is dependent on that service, rather than just using a global variable created in an outside library.

It also helps if you need to configure your API/library, then you would only do it once in the service and everywhere else would reap the benefits.

That is just for your case, in a general case its better to have all your logic in a service so that it can be reused everywhere. Controllers are pretty much the glue in your application to stick the data (services) to the view

Comments

0

Why not simply have my Angular code call my drawing function the way it is now instead of putting it in a service?

This is similar to asking why should we follow the MVC structure when we can have all the code in one place, in one file.

I guess the point is to make services do the "work" stuff while the controller server just to "connect" the work results with the presentation. Separation of code by functionality - your documentation is telling it right. And yes, sometimes it doesn't make sense to do things like that and it seems like an overkill... and sometimes it is, but most of the times it's very useful not only for forcing yourself to learn it the proper way, but also to have less headache in case your existing application needs to have additional/modified functionality, using it in multiple controllers/app, etc... and to avoid that "I wish I did it the right way in the beginning" scenario.

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.