Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between provider and instances in Angular?

Tags:

I am new to Angular. I am studying config block and run block of modules.

Please have a look at below code:

angular.module('myModule', []).
config(function(injectables) { // provider-injector
// This is an example of config block.
// You can have as many of these as you want.
// You can only inject Providers (not instances)
// into config blocks.
}).
run(function(injectables) { // instance-injector
// This is an example of a run block.
// You can have as many of these as you want.
// You can only inject instances (not Providers)
// into run blocks
});

As you can see in the config block it is written: "You can only inject Providers (not instances)".

What does this mean? Could anyone please explain what is the the difference between provider and instances?

like image 255
RajSharma Avatar asked Aug 09 '16 04:08

RajSharma


People also ask

What are instances in Angular?

When Angular creates a new instance of a component class, it determines which services or other dependencies that component needs by looking at the constructor parameter types. For example, the constructor of HeroListComponent needs HeroService .

What is a provider in Angular?

A provider is an instruction to the Dependency Injection system on how to obtain a value for a dependency. Most of the time, these dependencies are services that you create and provide. For the final sample application using the provider that this page describes, see the live example / download example .

What is the difference between a provider a service and a factory in Angular?

The major difference between an AngularJS service and an AngularJS factory is that a service is a constructor function and a factory is not. That is why, in the case of a factory, we return an object literal instead of using this.

What is the difference between providers and providedIn in Angular while using dependency injection?

An Injectable configured with providedIn will be tree-shaken if it is not injected by any (eagerly or lazy loaded) class in its assigned injector scope. However, an Injectable assigned to a providers array in some module/component will never be tree-shaken, even if it is not injected anywhere.


1 Answers

Actually your question is good. To make it very simple, we define services in Angular JS to achieve our features. Provider is one of the way to configure how that services should work. There are some more concepts namely Values, Constants, Factory, Service and Decorator in Angular JS, which can help us intercept the services in different manners. Please check the below link.

https://docs.angularjs.org/guide/providers

Coming back to the Providers, they are used to define application wide configurations that needs to be done even before application starts. Since config blocks are executed before the Angular JS modules are loaded we configure providers under them. Since the modules would not have been loaded by that time you can't access services inside a config block.

Run blocks are executed once all the modules are loaded by the $injector. Once you enter a run block, you are not allowed to configure your provider any more since your services will be loaded anyway. That's the reason you can't access providers inside a run block.

Let's see an example. I have designed my application to support both user and admin screens. But the features related to them are defined in their respective services. I want to load only the appropriate services when a user logs in. We achieve that using a provider as below.

Defining rolesProvider

myApp.provider("roles", function rolesProvider(){
var role;
this.setRole = function(value) {
role = value;
}

this.$get = function rolesFactory() {
if(role === "user") {
return new userRole();
} else {
return new adminRole();
}
}
});

Configuring rolesProvider as a user

myApp.config(["rolesProvider"], function(rulesProvider){
rulesProvider.setRole("user"); 
});

My application will be configured to run as a user rather than as an admin when the application kicks off.

Let me know if you need more explanations.

like image 66
Vijayarajan Ravindran Avatar answered Sep 23 '22 16:09

Vijayarajan Ravindran