jump to navigation

Concept of AngularJS Service & Controller (Note) January 17, 2014

Posted by Terry.Cho in Uncategorized.
Tags: , , ,
add a comment

The service is some kinds of library which can be injected by using DI.
The service is singleton and binded to module
Angular injects dependencies using “constructor” injection.
and when it tried to use the service.

The service is injected as a parameter in constructior or it can be injected by using injection annotation ($inject)
The service can be injected to other services or controller

* Service dependency injection to Controller
angular.module(‘invoice2’, [‘finance2’])
.controller(‘InvoiceController’, [‘currencyConverter’, function(currencyConverter) {
The controller import module named ‘finance2’.
In Invoice Controller it tried to use ‘currencyConverter’, so the service is refered by the controller like “controller(‘InvoiceController’, [‘currencyConverter’, ”
and the try to use currencyConverter the parameter also has been passed to constructor function like this “function(currencyConverter)”

* Controller Parameter
the ‘servicename’ is the service name will be injected to the controller
You can also alias service name in function like
[‘$scope’, ‘angularFire’, function(skop, af) {}]
the service $scope will be mapped to skop and angularFire will be mapped to af.
But is is not recommended to change the service name.
※ reference http://stackoverflow.com/questions/19238191/understanding-angular-js-controller-parameters