1
00:00:04,270 --> 00:00:06,310
Hello, everyone, and welcome back.

2
00:00:06,820 --> 00:00:12,430
The last lesson we wrote, our own provider for the cause is service dependency, and we have defined

3
00:00:12,430 --> 00:00:14,930
here a unique injection token.

4
00:00:15,070 --> 00:00:21,160
Now, notice that normally we don't need to write these functions or provide injection tokens.

5
00:00:21,400 --> 00:00:23,710
It all works in a much simpler way.

6
00:00:23,860 --> 00:00:30,190
So let's see exactly what Engelhard is doing under the hood when we are not using the inject decorated

7
00:00:30,190 --> 00:00:31,640
or manual profiler's.

8
00:00:31,960 --> 00:00:39,240
So how does Angela know, for example, what dependency to inject here without using here to inject

9
00:00:39,250 --> 00:00:40,020
liquor there?

10
00:00:40,120 --> 00:00:44,500
Because our previous version of the program worked fine without it?

11
00:00:44,710 --> 00:00:46,390
Well, the answer is very simple.

12
00:00:46,420 --> 00:00:52,390
What's going on is that angular supports as injection tokens, class names.

13
00:00:52,540 --> 00:00:59,380
So we here in the provide function if instead of creating here a manual injection token, we specify

14
00:00:59,380 --> 00:01:01,270
here the name of the class.

15
00:01:01,480 --> 00:01:06,310
These will also work as a unique identifier for a dependency.

16
00:01:06,520 --> 00:01:07,540
Let's try this out.

17
00:01:07,540 --> 00:01:12,710
If we reload here, the application, we're going to see that everything is still working as expected.

18
00:01:12,730 --> 00:01:17,070
So this is one of the key features of the angular dependency injection system.

19
00:01:17,080 --> 00:01:23,220
It's possible to identify dependencies using class names as a unique identifier.

20
00:01:23,380 --> 00:01:30,940
It's possible to use a class name as a dependency injection token because the last name exists at runtime

21
00:01:30,940 --> 00:01:33,430
under the form of a constructor function.

22
00:01:33,460 --> 00:01:38,650
On the other hand, we would not be able to use an interface as an injection token.

23
00:01:38,770 --> 00:01:42,780
And that's because an interface does not exist at runtime.

24
00:01:42,790 --> 00:01:45,300
It's a compile time construct only.

25
00:01:45,490 --> 00:01:51,060
And the other thing that we don't need to do is to write our own manual provider function.

26
00:01:51,220 --> 00:01:58,120
And the reason for that is that if to the angular dependency injection system, we pass these use class

27
00:01:58,120 --> 00:02:05,110
property instead of use factory and the dependencies and we specify here the name of a class in this

28
00:02:05,110 --> 00:02:12,010
case causes service, then Angular knows that it needs to call the new constructor on this class and

29
00:02:12,010 --> 00:02:16,910
it needs to provide the dependencies that are needed by the constructor.

30
00:02:17,020 --> 00:02:20,110
So that's one thing that Angular already knows how to do.

31
00:02:20,140 --> 00:02:23,340
So we don't need to write this function either.

32
00:02:23,380 --> 00:02:26,260
And we can remove here the injection token.

33
00:02:26,440 --> 00:02:28,420
We won't be needing it anymore.

34
00:02:28,510 --> 00:02:35,530
We don't need our manually written injection token anymore because Angular internally is using the class

35
00:02:35,530 --> 00:02:39,040
name as a unique identifier for the injectable.

36
00:02:39,160 --> 00:02:43,800
Notice that this configuration in its current form has some redundancy.

37
00:02:43,810 --> 00:02:47,560
We are using the courtesy service class on multiple places.

38
00:02:47,800 --> 00:02:51,750
This can be further simplified into the following notation.

39
00:02:51,760 --> 00:02:58,060
We are going to remove here the complete configuration object and we're going to pass in here only the

40
00:02:58,090 --> 00:03:00,130
class name and nothing more.

41
00:03:00,250 --> 00:03:01,660
With this notation.

42
00:03:01,660 --> 00:03:07,960
Angular knows that this is the unique identifier of the dependency is being used as an injection token.

43
00:03:08,080 --> 00:03:15,080
And also this is a constructor function that allows ANGULAR to instantiate the dependency itself.

44
00:03:15,100 --> 00:03:21,640
So with this, the dependency injection system has all the necessary information for knowing how to

45
00:03:21,640 --> 00:03:22,510
create here.

46
00:03:22,600 --> 00:03:24,770
The course is service dependency.

47
00:03:25,130 --> 00:03:26,800
Now notice the following.

48
00:03:26,800 --> 00:03:33,610
So far we have been using here this provider's property of the component so each component can specify

49
00:03:33,610 --> 00:03:35,350
its own provider's list.

50
00:03:35,380 --> 00:03:39,640
This is not the usual way that we define application wide singletons.

51
00:03:39,880 --> 00:03:43,870
Usually we define them here at the level of the application module.

52
00:03:43,960 --> 00:03:51,350
So let's learn exactly what does it mean for dependency to be creative, the level of a given component.

53
00:03:51,370 --> 00:03:54,810
Let's talk about hierarchical dependency injection.

54
00:03:54,850 --> 00:03:57,550
This is coming right up in the next lesson.

