You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: expand example that removes value accessor provider
In the form field guide, we instruct people to remove the `NG_VALUE_ACCESSOR` provider and replace it with injecting the `NgControl`, however it's not very explicit and is easy to miss. These changes expand the example a bit in an effort to make it harder to miss.
Fixes#8158.
This property allows the form field control to specify the `@angular/forms` control that is bound to this component. Since we haven't set up our component to act as a `ControlValueAccessor`, we'll just set this to `null` in our component.
162
+
This property allows the form field control to specify the `@angular/forms` control that is bound
163
+
to this component. Since we haven't set up our component to act as a `ControlValueAccessor`, we'll
164
+
just set this to `null` in our component.
163
165
164
166
```ts
165
167
ngControl: NgControl=null;
166
168
```
167
169
168
-
It is likely you will want to implement `ControlValueAccessor` so that your component can work with `formControl` and `ngModel`. If you do implement `ControlValueAccessor` you will need to get a reference to the `NgControl` associated with your control and make it publicly available.
170
+
It is likely you will want to implement `ControlValueAccessor` so that your component can work with
171
+
`formControl` and `ngModel`. If you do implement `ControlValueAccessor` you will need to get a
172
+
reference to the `NgControl` associated with your control and make it publicly available.
169
173
170
-
The easy way is to add it as a public property to your constructor and let dependency injection handle it:
174
+
The easy way is to add it as a public property to your constructor and let dependency injection
175
+
handle it:
171
176
172
177
```ts
173
178
constructor(
@@ -177,19 +182,39 @@ constructor(
177
182
) { }
178
183
```
179
184
180
-
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.
185
+
Note that if your component implements `ControlValueAccessor`, it may already be set up to provide
186
+
`NG_VALUE_ACCESSOR` (in the `providers` part of the component's decorator, or possibly in a module
187
+
declaration). If so you may get a *cannot instantiate cyclic dependency* error.
181
188
182
189
To resolve this, remove the `NG_VALUE_ACCESSOR` provider and instead set the value accessor directly:
183
190
184
191
```ts
185
-
constructor(
186
-
...,
187
-
@Optional() @Self() publicngControl: NgControl,
188
-
...,
189
-
) {
190
-
// Setting the value accessor directly (instead of using
191
-
// the providers) to avoid running into a circular import.
192
-
if (this.ngControl!=null) { this.ngControl.valueAccessor=this; }
0 commit comments