-
Notifications
You must be signed in to change notification settings - Fork 6.8k
updated Creating a Custom Form Field doc ngControl section #9855
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
Conversation
…lect errors injecting NgControl If the component already implements ControlValueAccessor, injecting NgControl in the constructor (as described in the docs) may fail. Added an alternate way.
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
Signed CLA. |
CLAs look good, thanks! |
// ... | ||
|
||
ngAfterViewInit() { | ||
this.ngControl = this._injector.get(NgControl); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that this is the proper way to handle it either. Instead of providing the NG_VALUE_ACCESSOR
, you can set the accessor the same way we do for mat-select
. See https://github.com/angular/material2/blob/master/src/lib/select/select.ts#L489
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I saw two approaches and picked the other one. Not knowing enough about Angular internals it’s hard to say which is better. I’ll revise.
Revision added. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rellis-of-rhindleton Awesome, thanks for this! one small wording request and then I'll mark it as ready to merge
``` | ||
|
||
Note that if your component implements `ControlValueAccessor`, it may already be set up to provide `NG_VALUE_ACCESSOR` (in the `providers` part of the component's decorator, or possibly in a module declaration). If so you may get a *cannot instantiate cyclic dependency* error. | ||
|
||
To resolve this, remove the `NG_VALUE_ACCESSOR` and instead set the value accessor directly: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...remove the NG_VALUE_ACCESSOR
provider and...
Good call, done. |
* Creating a custom form field docs -- updated ngControl section to reflect errors injecting NgControl If the component already implements ControlValueAccessor, injecting NgControl in the constructor (as described in the docs) may fail. Added an alternate way. * Update creating-a-custom-form-field-control.md * added word "provider" for clarity
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
If the component already implements ControlValueAccessor, injecting NgControl in the constructor (as described in the docs) may fail. Added an alternate approach to get past this problem.