Search code examples
javascriptangularjsangular-strap

AngularJS Providers


I'm studying providers in AngularJS but I am a beginner in AngularJS and not very experienced in javascript.
I was looking in AngularStrap code and found that the modal provider (src\modal\modal.js) returns a function instead of an object:

this.$get = function($window, $rootScope, $bsCompiler, $animate, $timeout, $sce, dimensions) {

    ...

    return ModalFactory;
};

Why is the function returned instead of the concrete object.
In my understanding, in this case, the service can't act like a singleton, please correct me if I'm wrong.


Solution

  • Angular service instances can be anything (except service service because it uses constructor function to create an object with new).

    The value returned by $get is saved as service instance when the service is being injected the first time. This instance is returned during all subsequent dependency injections within the same app, $get won't be called again, that is why the services are singletons.