Angular dependency injection

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

Angular 2 Tutorial playlist

Angular 2 Text articles and slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this video we will discuss
1. What is Dependency Injection
2. How dependency injection works in angular

Let us understand Dependency Injection in Angular with an example. Consider this piece of code in EmployeeListComponent.
export class EmployeeListComponent implements OnInit {

private _employeeService: EmployeeService;

constructor(_employeeService: EmployeeService) {
this._employeeService = _employeeService;
}

ngOnInit() {
.subscribe(
}

// Rest of the code
}

1. We register a service with the angular injector by using the providers property of @Component decorator or @NgModule decorator.

2. When a component in Angular needs a service instance, it does not explicitly create it. Instead it just specifies it has a dependency on a service and needs an instance of it by including the service as a constructor parameter.

3. When an instance of the component is created, the angular injector creates an instance of the service class and provides it to component constructor.

4. So the component which is dependent on a service instance, receives the instance from an external source rather than creating it itself. This is called Dependency Injection.

What is Dependency Injection
It's a coding pattern in which a class receives its dependencies from an external source rather than creating them itself.

So if we relate this definition to our example, EmployeeListComponent has a dependency on EmployeeService. The EmployeeListComponent receives the dependency instance (i.e EmployeeService instance) from the the external source (i.e the angular injector) rather than creating the instance itself.

1. Why should we use Dependency Injection?
2. What benefits it provide?
3. Why can't we explicitly create an instance of the EmployeeService class using the new keyword and use that instance instead in our EmployeeListComponent?

We will answer these questions in our next video.
Рекомендации по теме
Комментарии
Автор

Finally, I was able to understand dependency injection, no one can explain it like you. Thank you so much.

muralidharmoka
Автор

Wow, this was so clear and straight forward that you blew my mind. I have always struggled to understand DI but you cleared it up for me completely. TY!!

vicliur
Автор

Use very clear and precise language to explain this technical term which looks so profound and difficult. Excellent job. God bless you!

amyzeng
Автор

I have learnt lot of things from your great videos and way of explantion is too good.You are awesome...God bless you

rajneesh
Автор

Hi! I love all your videos, thanks for all you effort from Mexico

guillermopalaciosgaray
Автор

best video so far i got in internet about service and provider.

pastak
Автор

your voice has changed so much from 2012 O_O WOW

anna.bananna
Автор

a very clear explanation abaout a hard topic. You really rocks!!!

aaronaaronaaron
Автор

Hi Sir, Great and Easy explanation. Thank You very much

ravi.dannani
Автор

Please explain angular forms with CRUD. I have seen all videos , All are very helpful . Keep posting

sanjayrcm
Автор

kudvenkat is the best tutor in the world.

DecentProgrammer
Автор

wow, dependency injection, has been waiting for this topic. Kudvenkut God bless you !

khalidkhan
Автор

Hi Venkat, you say angular injector creates an instance of service when it is referenced in the component constructor. Is this correct for all the component constructors, I mean instant of the service created every time when a component constructor called?

anujguleria
Автор

Please add video for useClass, useExisting, useFactory, useValue and type provider and also for tree shakable provider

pushpakkalal
Автор

Sir, I have developed a web using your video, so far all good.
Now, I don't know how to publish using visual studio.
Please help me on it.
I want to deploy using File system and then I'll FTP to actual server.

bhavinjadav