Skip to content
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

Null parent form via FormGroupDirective from child form component #23914

Closed
acohenOT opened this issue May 15, 2018 · 2 comments
Closed

Null parent form via FormGroupDirective from child form component #23914

acohenOT opened this issue May 15, 2018 · 2 comments

Comments

@acohenOT
Copy link
Contributor

I'm submitting a...


[x ] Bug report  

Current behavior

This is based on the sub-form demo by @kara for reactive forms: https://youtu.be/CD_t3m2WMM8?t=2130

Using reactive form, a child custom form component is not able to access its parent form in order to add form controls to the parent. When injecting FormGroupDirective, the "form" property is null.

Expected behavior

When injecting FormGroupDirective and the component is part of a reactive form, the "form" property should give a reference to the parent's form.

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-53qnzx

Examine console:

PARENT FORM IS NULL
ERROR TypeError: Cannot read property 'addControl' of null

What is the motivation / use case for changing the behavior?

Adding child form to a parent form without requiring the parent form to create form controls for the child form elements.

Environment


Angular version: 6.0.1


Browser:
- [x ] Chrome (desktop) version 66.0.3359.170
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [x ] IE version 11
 

Others:

@ngbot ngbot bot added this to the needsTriage milestone May 22, 2018
@kara
Copy link
Contributor

kara commented May 29, 2018

FormGroupDirective.form is set through an input property, so it won't be set yet in the constructor of the nested form component. If you save the directive instead and call this.form.form in your hook, it should work as intended.

https://stackblitz.com/edit/angular-2b3zcz?file=src%2Fapp%2Faddress%2Faddress.component.ts

(The code in the slide was edited to this.form to fit slide width, but looks like it was misleading. Sorry about that.)

@kara kara closed this as completed May 29, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants