Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AngularJS An Introduction to Directives What is a Directive?

lihaoquan
lihaoquan
12,045 Points

Factories & Services in AngularJS

I couldn't really understand what factories and services are in AngularJS, they are called PROVIDERS, does it mean that they provide the necessary functions and data for the controllers to use and manipulate? If its so, then why remove functions and logic from controller and put it into a factory instead? Is it separated so that other controllers can use the factory too?

2 Answers

Abdi Ahmed
Abdi Ahmed
7,151 Points

Hi Li

Services are basically specific functionality that is encapsulated. Think of it like a function that holds specific functionality that you call whenever you want to execute that functionality anywhere in your program. Instead of just declaring, adding logic and invoking the function it works in a different way in Angular. You need to create the service by using either app.factory or app.service, and then add the functionality:

app.service('myService', function(){

//your service logic here  

});

When you want to use the service in your Angular app you need to used dependency injection and add a reference to your service in your controller and then you can invoke the functionality within your controller:

app.controller('myController', [$scope, myService, function($scope, myService){

$scope.myFunction = myService.customLogic();

}]);

The main reasons for removing the logic from the controller is to make your services reusable and available to other controllers. Also, it helps to make your code more readable and clean. Besides creating your own services you can use the builtin ones that Angular provides such as the $http service that lets you get data from external APIs, to use this you have to use dependency injection just like you did for your custom service/factory.

lihaoquan
lihaoquan
12,045 Points

Thanks for clearifying my doubts!

Igor Yamshchykov
Igor Yamshchykov
24,397 Points

You might want to remove functions from controllers to services in order to use them somewhere else. It just helps you to reduce code duplication. You can use services for keeping data that you need to pass between two controllers, for instance, or other elements on your page. I'm not sure is it a best practice or no, but I often use services to keep my business logic in, seems logic to me, because in that case you keep business logic separately from your controller and controller is just stands for some kind of glue between your model and view. Also when you keep your business logic in services you can build a good code structure and there's no mess in the controllers. So to answer your question you may keep your functions in services and then provide them for your controllers.