It says that Angular JS Services are singleton objects which are instantiated only once in app.
In terms of instantiation, how different is it from Angular JS factories or controllers?
Answers with a fiddle will be helpful.
There is no factory component in AngularJS. A factory is a function, passed to module.factory(), whose responsibility is precisely to create the service instance. This factory is called only once by Angular, to create the unique instance of the service. This unique instance, returned by the factory function, is then injected everywhere the service is needed.
So, in short, when you do
app.factory('foo', function() {
var service = {};
...
return service;
});
You're not defining a factory. You're passing a factory function to app.factory(), and this factory creates and returns the service instance. 'foo' is the name of the service.
A controller is not a singleton at all. Every time ng-controller="FooCtrl" is used, or FooCtrl is used in some route or directive, a new instance of FooCtrl is created.
Service is a class or an object, which works on singleton pattern once you create an object of a class the same object will be reused so that the memory utilization is very less.
Related
In my AngularJS application I want to initialize a factory service, without calling it from a controller or other service. How can this be done?
The reason is that I want the service to call one of its functions when the application loads.
You could just inject your service in the angular.module(...).run(...) function.
Then you could add the required code in the Service's main function/constructor.
But just injecting the Service without using it is a bad practice, because you or someone could remove it some day without knowing why it was injected.
So the best way to me is to inject the service in the .run function AND call a yourService.init() method that you will expose from your Service.
That would be more explicit.
app.run(['yourService', function(yourService) {
yourService.init();
}]);
I am building an angularjs application, that has various controllers. For two of the controllers I am dependency injecting the same service. Is a separate instance of that service created for each of the controllers where it is being injected?
Short Answer
From Docs
Lazily instantiated – Angular only instantiates a service/factory when
an application component depends on it.
Singletons – Each component
dependent on a service gets a reference to the single instance
generated by the service factory.
Angular service/factory/provider are Singleton object. Which create its instance only single time and if you ask for instance again(by injecting dependency) it gives back the old instance which was already created.
You could go throw this long answer which has everything explained
Can I use databinding for dependency injection? I'd like to pass the services I want to call on the JSON file being delivered to the AngularJS application.
app.directive('directiveName', ['{{ Data.servName }}','lookupService', function () {};])
Can I use databinding in this method?
No, and that sounds like a terrible idea. Your REST api shouldn't be tightly coupled like this to your angular client implementation.
Anyway, if you has the name of a service and want to get this service instance, inject the $injector service, and call its get() method with the service name as argument.
what is the correct way to do constructor injection in angularjs for instantiating objects with a factory or a service and invoke in a controller?
Ref : http://docs.angularjs.org/guide/providers
If you could provide an example it would be really helpful.
An angular service is a singleton, so whatever you attach to the execution context in the service (eg: this.*), is usable in the module it's attached to.
An angular factory is a little different. In an angular factory, you are going to want to return a handle to a factory that will run your constructor function.
Here is a plunk demonstrating both of them.
If you are writing your own angular module, your services and factories would attach to the module the same way, except, you must then inject your module into the app module.
var myAppModule = angular.module('myAppModule', ['ngRoute', 'myCustomModule'])
This will allow the myAppModule to then use all the services, factories, directives in ngRoute and myCustomModule (myCustomModule is any custom module you write and inject into your app).
I like that Durandal has Activate/Deactivate methods that get called on the VM and that the views can even be cached.
Is there a way to have this same features in Angular?
You can use a service / factory. These are singletons in AngularJS and you can inject them to the controller. Strictly have one service per controller and the service will be maintained between navigations to the view / controller.
http://docs.angularjs.org/guide/dev_guide.services.creating_services
The way this would work is that your controller would take myService as an injected dependency. Then you assign it to $scope as $scope.cache = myService. Then in your view you bind to cache.someProperty for elements that wanted cached.
Just as an aside:
You can even share services containing state data between controllers. Although I would recommend against this pattern, because it looks like global state. Services you share between controllers should ideally only provide functionality instead of being data objects. Unless you are specifically looking for a global data store :)