jump to navigation

$ (dollar) prefix for angularJS January 17, 2014

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

$ prefix in angularJS
It is for angularJS predefined service only.
It is just a naming convention from the below snippet http://docs.angularjs.org/tutorial/step_05

‘$’ Prefix Naming Convention
You can create your own services, and in fact we will do exactly that in step 11. As a naming convention, angular’s built-in services, Scope methods and a few other angular APIs have a ‘$’ prefix in front of the name. Don’t use a ‘$’ prefix when naming your services and models, in order to avoid any possible naming collisions.

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
[‘servicename’,’servicename’….,function(servicename,servicename)]
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