-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
formControlName could not be used with component transclusion #13761
Comments
pls add a repro |
@asfernandes This unfortunate behavior is due to the fact that the What I'm usually doing in such case is using This is the best solution I've come so far. |
I came up with this other approach of duplicating the formGroup directive on the container component and on the form and it seems to work ok: https://plnkr.co/edit/77wcSk0dnDKuJ2IyBgZd?p=preview |
@gschuager Indeed this is valid solution too. It will work as the I was hoping that the angular team will address this behavior somehow as it has another side effect. You can check this issue here. Unfortunately it was closed and no answer since then 😞. |
I dig into the angular source code in found a related issue with good menu example: #5126 |
As others have mentioned, the problem here is that child form directives like It's possible to make this work with a mix of viewProviders and a template-outlet, but it's hacky and we should have an easier way to implement components like this. This needs a design doc. |
+1 |
Does anyone have a new solution / workaround for this? When duplicating the Here's a small Stack Blitz example: If you click the "submit", then the errors for the first input will be shown, but the second input doesn't get triggered |
Any update? @kara can you elaberate on how to make it work with viewProviders and a template-outlet? |
@benneq when removing |
@Russoturisto we have the same requirement. We are wrapping material form components. See my solution on SO which works very well for us: |
@kara , @DzmitryShylovich, @petebacondarwin ping? Are there any plans to fix / improve this issue? |
@steve-todorov - at the moment the Angular team are all hands to the pumps on getting the new Ivy rendering engine and associated parts working. I would not expect this issue to be addressed until that is complete, at the earliest. @kara might have more info. |
I see this as a good time to investigate it. This fix can be related to rendering. |
@petebacondarwin many thanks for replying! Looking forward to Ivy as well! I'm sure you guys will do an awesome job! :) |
We are also wrapping material components to reduce boiler plate and are blocked on this issue as well. Will be hoping this is looked at some point as well. |
Here is a solution: <CustomForm [formGroup]="group">
<div [formGroup]="group">
.... elements here
</div>
</CustomForm> Just add it to a div as well |
Just sharing that @rusev solution worked great for me, after changing slightly for new apis: https://next.plnkr.co/edit/IfXXzwQk1uhUJTmL For whatever reason (probably just what other stuff i have going on with this app) I was not able to get @gschuager approach to work for me, but suspect its just the composition of my forms im assuming this is still the 'common approach' to this problem |
The sanest and most straight-forward/obvious workaround for this was to pass in the reference to the form along with the key in the So instead of <ng-template
[ngTemplateOutlet]="tpl"
[ngTemplateOutletContext]="{
key: 'path.to.key'
}"
></ng-template> pass in the form as well <ng-template
[ngTemplateOutlet]="tpl"
[ngTemplateOutletContext]="{
key: 'path.to.key',
form: form
}"
></ng-template> Inside the template, instead of The |
viewProviders have solved my problem with wrapper component for inputs in reactive forms. Example:
usage: "name" is used as formControlName in wrapper component |
But is Validation and Blur Handling still working properly for „name“?
… Am 06.09.2019 um 18:37 schrieb darko99x ***@***.***>:
viewProviders have solved my problem with wrapper component for inputs in reactive forms. Example:
@component({
selector: 'app-input',
templateUrl: './input.component.html',
styleUrls: ['./input.component.scss'],
viewProviders: [
{
provide: ControlContainer,
useExisting: FormGroupDirective
}
]
})
usage:
<app-input name="lastName">Last name</app-input>
"name" is used as formControlName in wrapper component
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Yes, that is just shorter naming convention for my directives. Component attaches itself to the parent formGroup and it works as expected. Validation, blur, dirty, submit, valueChange... whole nine yards |
I can manage to get However my big problem is when I've tried using The closest I've got so far is to have the label, input and validation messages of the control as a component which implements
Any ideas? |
In case anyone cares, I managed to get rid of the repetition part by using templating in combination with content projection, both together (couldn't use content projection alone as you end up with multiple children, one for each switch case, couldn't use templating alone as you can't grab HTML from a separate file). |
I found a workaround using a factory to make it work with formGroupNames and FormGroupDirectives in Angular 7. It feels hacky, but it is the best solution I have found so far.
|
@asfernandes you can transclude a component that has a reference to a example look at the edit: 31-12-2019 |
I have switched to React long long time ago. |
Good for you if you like it. I am the author of: https://www.wearefrontend.com |
That I flagged in "Jan 3, 2017". I do open source development as well, and my comment was not insulting. |
There's nothing insulting in saying that someone is using a different framework now. The issue was opened 3 years ago and you tagged him directly with the solution. He was kind enough to reply and tell you that he won't be able to check it out because he uses React now. Then for no reason you decided to call him out in a patronizing tone. What did you expect him to do? Open 3 years old code and decide to rewrite the whole the app to Angular? |
Thanks @nasreddineskandrani, but my problem is that my input wrapper is sometimes included under a nested FormGroup, therefore ControlContainer sometimes is a FormGroupDirective and sometimes a FormGroupName. In order to make it work I have to declare something like this:
It feels wrong to have to use to a factory to provide the ControlContainer. I also don't understand why I need to expose the ControlContainer in viewProviders. If I expose it in providers it doesn't work. |
@lazarljubenovic His answer seems to say "i switched to react so it's not my problem anymore". I don't like this kind of mindset so i just said what i think about it. It's just my opinion you can agree or disagree with my answer knowing that i took the time to try to answer him (providing an online example) when i am currently working in a project with "react"... note: what lead me here -> in our spare time (with my friends), we are comparing angular forms with react ones and we are trying to solve issues in angular forms listed in #31963 @undo76 if possible can you share a stackblitz (online) with your case/solution? |
What I am trying to do is to create a wrapper for inputs with labels, errors, styles. In order to make it work in nested groups with FormGroupName I have to propagate the current ControlContainer in a factory. |
@undo76 thanks for the example. I understand what you did. It's a really nice solution but if i am not wrong this is a different problem/case from the initial one that @asfernandes opened the issue for? |
@nasreddineskandrani, you are right. My answer was directed to @adam-marshall question about nested named groups. On the other hand, I don't think mine is a nice solution. It is not obvious at all why I need to do so for such a simple wrapper. |
@undo76 oki +1 . In my opinion, what will be cool is to isolate your case in another angular issue. So devs and angular core team can evaluate your solution and give you feedback or add a new feature/fix to angular to do it with a simpler way. Also it'll help the readers of this issue to stay focused on the different answers to the initial issue (that multiple devs proposed already on top). |
For everyone having this problem: Had the same difficulty as @artem-v-shamsutdinov while attempting to wrap angular material components into my own app components. Solved this by adding a @input prop at my input component to carry the current formGroup instance, and adding it to my input container tag. login.component.html: <form [formGroup]="loginForm">
<cv-classic-input [...] [parentForm]="loginForm"></cv-classic-input>
</form> cv-classic-input.component.html: <mat-form-field [...] [formGroup]="parentForm">
<mat-label>{{label}}</mat-label>
<input [...] [formControlName]="name">
</mat-form-field> cv-classic-component.ts: [...]
export class CvClassicInputComponent implements OnInit {
@Input() parentForm: FormGroup
}
[...] Hope this helped. |
@renanmontebelo You don't need a custom input at all, you can simply inject it. See here for details: https://stackoverflow.com/a/46025197/2131286 Also the issue is about projection, not sub-components. |
@lazarljubenovic I believe you tagged the wrong Renan :-) |
Whopes, sorry for the ping. 😅 @RenanRossiDias ☝️ |
Somebody have some news about it? I have try with the last angular 11, but not yet work :( |
@lazarljubenovic
when in his approach he ends up with:
two form group vs one form group no? @seeiuu |
I'm not sure where you got the extra group from. |
@lazarljubenovic your example in stackoverflow has an extra formgroup but it was about |
I'm running into this while creating reusable forms. Any movement? |
+1 |
Version: 2.4.1
A component with selector parent-component has this:
And it is inserted in another template with:
Angular says the control is used without a form.
The text was updated successfully, but these errors were encountered: