Monday, March 17, 2014

Angularjs Create Module, Angularjs Creating module

Angular Create Module

I think (personal opinion) it would have been a lot clearer if we had a separate call for creating a module but the current way to do that is to provide a second parameter to angular.module. The second parameter to angular.module is an array of modules on which the current module will depend on.
Module syntax:
var myModule = angular.module( 'moduleName', [ 
 'dependency1' ,
 'dependency2' 
]);
You should note that if you do create another module with a module name that has already been created earlier, then the old module will be overwritten – and all the registered factories etc on that module will be lost. This is a common problem I have noticed people get into.

Factory

A factory is probably the used and the most common used of the three.
Syntaxmodule.factory('factoryName', function);
Result: When declaring factoryName as an injectable argument you will be provided thevalue that is returned by invoking the function reference passed to module.factory. In other words, on every call the arguments passed in again.
Usage: Could be useful for returning a ‘class’ function that can then be new’ed to create instances.

Service

A service is quite similar to a factory. There are some small differences however.
Syntaxmodule.service(‘serviceName’, function);
Result: When declaring serviceName as an injectable argument you will be provided with the instance of a function passed to module.service. This differs from the factory
Usage: Could be useful for sharing utility functions that are useful to invoke by simply appending () to the injected function reference. Could also be run with injectedArg.call(this) or similar.

Provider

Provider is the most configurable of all three.  All other providers are derived from it. It enables us define how the service will be provided even before the injection system is in place. This is achieved with configure call where we register work during the modules configuration phase.
Syntax: Syntax: module.provider( ‘providerName’, function );
Result: When declaring providerName as an injectable argument you will be provided the value that is returned by invoking the $get method of the function reference passed to module.provider.
Usage: Could be useful for returning a ‘class’ function that can then be new’ed to create instances but that requires some sort of configuration before being injected. Perhaps useful for classes that are reusable across projects.

What to choose

As usual, it depends. The factory has the Revealing Module pattern which gives the possibility to hide private variables. This can be very useful. However if you don’t have them and the injection of the service isn’t a problem for you, the service can be useful for you as well. The provider is the last in line to use. In most cases you won’t use this one. Use this only when you need the config part. This can be very handy when developing services that were shared of applications.

No comments:

Post a Comment