When I first wrote about the differences between services and factories in Angular, the bottom line was that the difference is semantic and so you should just pick one and stick with it.
Back then I made the point that, personally, I find factories require less boilerplate code and so they are often my choice.
Since then, a use case for using services has become more common, and that’s when using ES6/ES2015 classes.
When making use of classes, services are the right choice.
That’s because classes map naturally to an Angular service, let’s see how.
Take a look at this non-ES6
1 2 3 4 5 6
Converting this to an ES6 class would look like this:
1 2 3 4 5 6 7 8
The registration line would then be changed to this:
The behavior for all existing code that injects
UsersStore will remain the same, this is a seamless transition.
An important note, though, is the way injection is now performed.
$http is now provided to the class’s
In order to be able to access the injected service later, for example in the
getAll method, we have to save it as a member.
Then, the code in the converted
getAll function should make sure to reference it as
The extra typing around a constructor might put off some people, but looking at the bright side I find it helps making it more painful to write uber controllers and services that take on too much responsibility :)
Subscribe below to learn more about modern Angular and using Angular with ES6.
You want to do Angular the right way.
You hate spending time working on a project, only to find it’s completely wrong a month later.
But, as you try to get things right, you end up staring at the blinking cursor in your IDE, not sure what to type.
Every line of code you write means making decisions, and it’s paralyzing.
You look at blog posts, tutorials, videos, but each is a bit different.
Now you need to reverse engineer every advice to see what version of Angular it was written for, how updated it is, and whether it fits the current way of doing things.
What if you knew the Angular Way of doing things?
Imagine knocking down your tasks and spending your brain cycles on your product’s core.
Wouldn’t it be nice to know Angular like a second language?
You can write modern, clean and future-ready Angular right now.
Sign up below and get more of these helpful posts, free!
Always up to date and I’ve already done all the research for you.
And be the first the hear about my Modern Angular 1.x book – writing future proof Angular right now.