114. How many Instances of Service Should It Be
Jul 15, 2026 04:41
· 2:12
· English
· Whisper Turbo
· 2 Reproduktory
Platnosť tohto prepisu vyprší dnes.
Upgrade na trvalé ukladanie →
Zobrazuje sa len
0:02
S…
Speaker 2 (114. How many Instances of Service Should It Be)
In the last lecture,
0:03
S…
Speaker 2 (114. How many Instances of Service Should It Be)
we learned about that hierarchical injector.
0:06
S…
Speaker 2 (114. How many Instances of Service Should It Be)
And actually,
0:07
S…
Speaker 2 (114. How many Instances of Service Should It Be)
this injection behavior leads to the behavior we see here.
0:11
S…
Speaker 1 (114. How many Instances of Service Should It Be)
Now,
0:11
S…
Speaker 2 (114. How many Instances of Service Should It Be)
that's not a bug that's intended.
0:13
S…
Speaker 2 (114. How many Instances of Service Should It Be)
You might have an application where you want to have many different
0:17
S…
Speaker 2 (114. How many Instances of Service Should It Be)
instances of the same service,
0:19
S…
Speaker 2 (114. How many Instances of Service Should It Be)
where you absolutely don't want to have the same instance.
0:23
S…
Speaker 2 (114. How many Instances of Service Should It Be)
But sometimes you want to have the same instance.
0:25
S…
Speaker 2 (114. How many Instances of Service Should It Be)
And we definitely do.
0:27
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Because right now for our account service,
0:30
S…
Speaker 2 (114. How many Instances of Service Should It Be)
we have three instances.
0:32
S…
Speaker 2 (114. How many Instances of Service Should It Be)
The first one gets created here in AppComponent.
0:35
S…
Speaker 2 (114. How many Instances of Service Should It Be)
We provide it here.
0:36
S…
Speaker 2 (114. How many Instances of Service Should It Be)
AppComponent receives its own service or its own instance of the service,
0:40
S…
Speaker 2 (114. How many Instances of Service Should It Be)
I should say, as do all the child components.
0:43
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Now you might say,
0:44
S…
Speaker 2 (114. How many Instances of Service Should It Be)
fine, because new account and account are child components,
0:48
S…
Speaker 1 (114. How many Instances of Service Should It Be)
right?
0:49
S…
Speaker 2 (114. How many Instances of Service Should It Be)
But there we provide it again,
0:50
S…
Speaker 2 (114. How many Instances of Service Should It Be)
and hence we override the instance we would get from the app component.
0:54
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Now new account and account component have
0:58
S…
Speaker 2 (114. How many Instances of Service Should It Be)
their own instances.
0:59
S…
Speaker 2 (114. How many Instances of Service Should It Be)
So these are the other two instances of this service class.
1:02
S…
Speaker 2 (114. How many Instances of Service Should It Be)
And therefore,
1:04
S…
Speaker 2 (114. How many Instances of Service Should It Be)
if we add a new service,
1:06
S…
Speaker 2 (114. How many Instances of Service Should It Be)
we call add account,
1:07
S…
Speaker 2 (114. How many Instances of Service Should It Be)
there we push it on this array of accounts,
1:10
S…
Speaker 2 (114. How many Instances of Service Should It Be)
and this actually works,
1:11
S…
Speaker 2 (114. How many Instances of Service Should It Be)
but it's a totally different instance of the service than the one
1:15
S…
Speaker 2 (114. How many Instances of Service Should It Be)
we use here in app component,
1:17
S…
Speaker 2 (114. How many Instances of Service Should It Be)
the one through which we loop.
1:19
S…
Speaker 2 (114. How many Instances of Service Should It Be)
How can we fix it then here for our app?
1:21
S…
Speaker 1 (114. How many Instances of Service Should It Be)
Simple.
1:22
S…
Speaker 2 (114. How many Instances of Service Should It Be)
We just remove it from the provider's array.
1:24
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Don't remove it from the constructor.
1:27
S…
Speaker 2 (114. How many Instances of Service Should It Be)
We of course do need it here,
1:28
S…
Speaker 2 (114. How many Instances of Service Should It Be)
because that tells Angular that we want an instance.
1:32
S…
Speaker 2 (114. How many Instances of Service Should It Be)
The provider's array basically tells which instance,
1:35
S…
Speaker 2 (114. How many Instances of Service Should It Be)
so there we needed to remove it.
1:36
S…
Speaker 2 (114. How many Instances of Service Should It Be)
And the same for account component.
1:38
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Leave it in the constructor,
1:40
S…
Speaker 2 (114. How many Instances of Service Should It Be)
leave the import at the top,
1:42
S…
Speaker 2 (114. How many Instances of Service Should It Be)
but remove it from the provider's array.
1:44
S…
Speaker 2 (114. How many Instances of Service Should It Be)
With that little change,
1:46
S…
Speaker 2 (114. How many Instances of Service Should It Be)
if we go back to the application after saving it,
1:48
S…
Speaker 2 (114. How many Instances of Service Should It Be)
let's add another account.
1:51
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Now you see it at the bottom.
1:53
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Now you can change the status again.
1:56
S…
Speaker 2 (114. How many Instances of Service Should It Be)
So this is the difference.
1:58
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Different instances of the service.
2:01
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Now we're using one and the same before we weren't.
2:04
S…
Speaker 2 (114. How many Instances of Service Should It Be)
Both may be the behavior you want in your app,
2:07
S…
Speaker 2 (114. How many Instances of Service Should It Be)
but make sure that you get the correct one.
Tento prepis bol vytvorený pomocou AI (automatického rozpoznávania reči). Môže obsahovať chyby. Pre kritické použitie si ho overte s originálnym zvukom. Politika AI
Zhrnutie
Kliknutím na položku Zhrnúť vygenerujete súhrn AI tohto prepisu.
Zhrnutie...
Opýtajte sa AI o tomto prepise
Opýtajte sa čokoľvek o tomto prepise - AI nájde príslušné časti a odpovie.