Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private angular service or controller

Tags:

angularjs

I have an angular module that contains some 'private' services. Those private services are only needed by other services in the same module and it makes sense to expose them for testing. But I don't want other modules to use this services.

Is there any possibility to mark such services 'private'? Is there at least a convention to name this services so that others recognize them as private?

like image 557
Thomas Koch Avatar asked Feb 23 '15 17:02

Thomas Koch


People also ask

Should services be private Angular?

Generally speaking, services should be injected as private. You'd only mark them as public if you want to access the service directly from the template, which leans towards bad practice.

Why we use private constructor in Angular?

It is about encapsulation, and when you have a field or method on your component that you want to encapsulate in it, making it clear that it shouldn't be accessed from anywhere else, then you should absolutely make it private : That's what private is for: It signals your intent that whatever you've put it on shouldn't ...

What is the difference between providedIn and providers in Angular?

What is the difference between providers:[ ] and providedIn? Essentially the same thing, just a difference in who tells the service where it should be injected.

What is the use of @injectable in Angular?

The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency. Likewise, the @Injectable() decorator indicates that a component, class, pipe, or NgModule has a dependency on a service. The injector is the main mechanism.


1 Answers

If the only reason you need these "classes" to be angular Services is to have their dependencies injected painlessly, you could use $injector.instantiate to instantiate them without registering them as services.

Sample Code:

var PrivateClass = (function () {
    function PrivateClass($log) {
        this.hello = function () { $log.debug("Hello World!");}
    }
    PrivateClass.$inject = ["$log"];
    return PrivateClass;
})();

angular.module('TestApp', []).run(['$injector', function ($injector) {
    var p = $injector.instantiate(PrivateClass);
    p.hello();
}]);

You would use $injector.instantiate(PrivateClass) in the constructor (or anywhere) within the services that need it, to create an instance of the PrivateClass. If you need PrivateClass to behave like a singleton (like a real angular service), you could use an accessor class that instantiate it once and returns its reference to callers.

The advantage of this is that you need not pollute the instantiating service's (in this case the function passed to angular.run), dependency array with dependencies that are only needed so they could be passed on to the PrivateClass ($log in this case).

I noticed that this is an year old question but, I found this while looking for a way to achieve exactly the above and ultimately solved it using this approach.

like image 186
Sameera Avatar answered Nov 15 '22 00:11

Sameera