The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Services, Factories, and Providers: Creating a Factory

Services, Factories, and Providers: Creating a Factory

3:53
Creating your own services in AngularJS can be confusing. What are the differences between an AngularJS module's Service, Provider and Factory functions? This lesson shows how Angular's factory function is just the JavaScript module design pattern.
Watch this lesson now
Avatar
egghead.io

Creating your own services in AngularJS can be confusing. What are the differences between an AngularJS module's Service, Provider and Factory functions? This lesson shows how Angular's factory function is just the JavaScript module design pattern.

Avatar
Ben

Why is it called a factory if we're not creating new objects?

In reply to egghead.io
Avatar
Sergey

Was hoping to learn if there are any particular scenarios to choose Service over Factory or vice versa.

Avatar
Craig

I still have a few more video to record and add to the playlist that go over the tradeoffs but for the most part it's more of a coding style thing than any actual performance or practical difference. For example, Service services lend themselves to using ES6/ES2015 classes or an object oriented style while Factories let you use a module or revealing module style of coding. That said if you want inheritance you would lean towards the service recipe.

In reply to Sergey
Avatar
Sergey

Thanks! Looking forward to seeng new episodes.

In reply to Craig
Avatar
Craig

Good question. But if you think about it you don't have to use the "new" keyword to create an object in JavaScript, you just need to use object literal syntax like this { }...so factories do create objects and return them.

In reply to Ben
Avatar
Paul

You can have unrevealed functions in a constructor function. Seems to me the main difference between service and factory is prototyping, ie service returns an instance with access to prototypical inheritance. Factory just calls a method which returns a plain object.

In reply to egghead.io
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?