How to inject controller into service using angular js? - angularjs

I am new to angular and want to know if I can inject controller into service and then that service can be shared into multiple services.
Or help me out to know how can I create a function in service which can be used my multiple controllers?

Related

AngularJS - Initialize service without calling it

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();
}]);

Is a new instance of service created for each controller in angularjs?

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

How to communicate scope data through multiple angular app?

I have separate login ng-app and index page ng-app
So how would I get LoginData scope into index ng-app?
you create a service and inject it as dependancy in your two controllers to share data between them.
a service is a singleton.
many source on google, search "angularjs share data between controllers" :
a good solution here
Share data between AngularJS controllers
You can do that using localStorage (HTML5) through angularJS

constructor injection in angularjs

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).

Can we write a custom angularjs service that is available to all modules but instantiated only once

Is it possible to write a custom angularjs service that is available to all modules on a page but instantiated only once?
Very much like the global services e.g. $http provided by Angular.
In AngularJS services are singletons by default, see the developer guide for details.

Resources