Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: controller vs service

I have read a couple of posts regarding proper usage of angularjs entities: services, factories, controllers and directives.

My particular concern is a comparison of a controller and a service. None of the posts though told me what is that a controller can do what service cannot and vice versa.

Can this be listed or is it just a matter of being canonical in angular's usage?

like image 315
user776686 Avatar asked Dec 10 '14 10:12

user776686


People also ask

What is a controller in angular?

A controller is a JavaScript Object, created by a standard JavaScript object constructor. The AngularJS application is defined by ng-app="myApp". The application runs inside the <div>.

How do I use an AngularJS service?

To use an AngularJS service, you add it as a dependency for the component (controller, service, filter or directive) that depends on the service. AngularJS's dependency injectionsubsystem takes care of the rest.

What is the use of HTTP in angular?

The $http Service. The $http service is one of the most common used services in AngularJS applications. The service makes a request to the server, and lets your application handle the response. Example. Use the $http service to request data from the server: var app = angular.module('myApp', []);

What is ng-controller="myctrl" attribute in AngularJS?

The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller. The myCtrl function is a JavaScript function. AngularJS will invoke the controller with a $scope object.


2 Answers

Controllers are typically used to be bound with a view. Controllers manage a view's life cycle, and should be thought of as View Controllers. A new controller will be created for each instance of a view, meaning that if you navigate away from a certain view, and then back again - or if you have more than once instance of a certain view, a new controller will be created each time.

Services are typically used as the business logic of your application. Services are similar to singletons in the sense that they are created once, and the instance is maintained throughout the entire life cycle of your application. It is a good place to put your logical functions which many views or components will require, and also hold global cache which needs to be accessed throughout multiple areas in your application.

like image 109
Gil Moshayof Avatar answered Oct 04 '22 18:10

Gil Moshayof


controllers - responsibilities: initialize the view, mediate interaction between view/scope and services. It has dependencies on the view and model, but is more concerned with the view and making it work.

services - responsibilities: provides business services that is not dependent on the view or the controller. Its primary concern is delivering services, regardless of the consumer (controller/view/other services).

I'm not convinced if persistence factors into the differences.

like image 24
pixelbits Avatar answered Oct 04 '22 16:10

pixelbits