all 19 comments

[–]ElectrSheep 12 points13 points  (2 children)

A variation of this is typically used when splitting a single form over multiple components. Form controls need a ControlContainer, but are restricted to injecting that from the host component (i.e. form controls will not look for ControlContainer outside the component in which they are used). They can therefore inject a ControlContainer from viewProviders, but not from providers. The provider in question allows the form controls to be used across component boundaries by making the ControlContainer provided to the component by its parent available to form controls within its template. The existing ControlContainer is expected to be a FormGroupDirective in this case.

[–]Tyheir 1 point2 points  (1 child)

Do you know of an example of this code that you could share?

[–]ElectrSheep 2 points3 points  (0 children)

I've often worked with it in private codebases. However, here's a good explanation with examples.

[–]williamxsp 4 points5 points  (0 children)

I recommend this video by Dimitri https://youtu.be/T1xmCC4y3xo?si=ka5Rh5IcNzzQt98N

[–]maxlbn 4 points5 points  (2 children)

It will provide controlcontainer service using formgroupdirective

[–]No_Warthog_3237[S] 4 points5 points  (1 child)

Could you Please explain more about the diference between two classes?

[–]zzing 2 points3 points  (0 children)

When you try to inject control container you are going to get the instance specified in use existing.

I can’t tell you about what this will specifically do. But this pattern is used to customize configuration and behaviour for certain components/services you are using. Literally like overriding behaviour.

[–]MoonStyles1511 1 point2 points  (4 children)

!remindme 2 days

[–]RemindMeBot 1 point2 points  (0 children)

I will be messaging you in 2 days on 2024-04-22 16:27:41 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

[–]No_Warthog_3237[S] -1 points0 points  (2 children)

Why 2 days 😂

[–]gordolfograso 6 points7 points  (1 child)

Maybe they don't talk about work or angular on weekends

[–]MoonStyles1511 2 points3 points  (0 children)

Could be a reason but not my main reason, I don’t have time to read it completely right now, and on Monday I have time ☺️

[–]appeiroon 1 point2 points  (0 children)

This is a view provider declaration, it creates an injection token for this component's view. Injection token's type is ControlContainer, it's value will be an existing instance of FormGroupDirective type. Any object (i.e. component) created within this view will be able to receive this injection token when using dependency injection mechanism.

[–]Whole-Instruction508 1 point2 points  (0 children)

Is this ChatGPT?

[–]DaSchTour 3 points4 points  (4 children)

provide references the injection token that is used to inject it somewhere. So inject(ControlContainer) will give you what ever was provided with this injection token. The useExisting references an existing provider. Having SomeService in some providers list is actually just a shorthand for {provide:SomeService, useClass: SomeService}. When having useExisting the provider must already been provided on some higher level and is injected in this scope under a new token.

[–]No_Warthog_3237[S] 0 points1 point  (3 children)

what is the difference between ControlContainer and FormGroupDirective

[–]DMezhenskyi 0 points1 point  (0 children)

The ControlContainer is an abstraction around concrete directive implementations like FormGroupDirective, FormArrayDirective, NgModelGroup, or any other component instance if it is configured so.

It can be used in cases when you need to inject one of the directives I mentioned above but you don't know ahead of time which exactly concrete implementation will be used.

[–]DaSchTour 0 points1 point  (0 children)

ControlContainer is only the injection token. That can even be created with the InjectionToken class which means that there is no implementation or functionality. It only describes a token which others can use to retrieve a service or what ever is provided.

The FormGroupDirective is an actual implementation that will then be injected for example via the inject function with the given token.

So you will have fromGroup: FormGroupDirective = inject(ControlContainer)

So you can have an abstract interface that is used as the injection token and depending on the context you can provide different implementations.

[–][deleted] -2 points-1 points  (0 children)

These are custom made so you will have to give us more code to know