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
Copy file name to clipboardExpand all lines: README.md
+53Lines changed: 53 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -258,3 +258,56 @@ setTimeout(() => {
258
258
```
259
259
260
260
See the code in this project [here](https://github.com/actionanand/angular-form/blob/master/src/app/auth/login/login.component.ts)
261
+
262
+
## Wiki
263
+
264
+
### Custom Form Controls `ControlValueAccessor`:
265
+
266
+
This interface lets you create custom input components that integrate seamlessly with Angular forms. You can define how your component reads and writes values to the form control, as well as how it handles validation and change detection.
267
+
268
+
### FormArrays:
269
+
270
+
Manage lists of form controls that can be added or removed dynamically.
271
+
272
+
### FormGroup:
273
+
274
+
Create nested form groups to represent complex data structures.
275
+
276
+
### Observables:
277
+
278
+
1. `valueChanges`: Use the valueChanges observable to track changes to form controls or the entire form.
279
+
2. `patchValue` and `setValue`: Update form control values programmatically.
280
+
281
+
### onSubmit:
282
+
283
+
Handle form submission events and process the form data.
284
+
285
+
### `ControlValueAccessor` (CVA) - Explanation
286
+
287
+
1. `NG_VALUE_ACCESSOR`: This provider tells Angular that your component can act as a CVA.
288
+
2. `writeValue`: This method is called by Angular when the form control's value changes.
289
+
3. `registerOnChange`: This method is called by Angular to register a callback function that will be called when the component's value changes.
290
+
4. `registerOnTouched`: This method is called by Angular to register a callback function that will be called when the component is touched.
291
+
292
+
The `NG_VALUE_ACCESSOR` is binding things to component's `:host` and linking to methods (`ControlValueAccessor` methods) there. Your module does not have any of those form methods (like `writeValue`, `registerOnTouched` etc). Your form element does. So providing at component level binds this for that specific element. Additionally, providing so deep down means each form control has it's own control value accessor and not a shared one.
293
+
294
+
Angular Form controls and its API is not the same as the DOM form controls. What angular does is binds to the inputs/outputs of the dom element and provides you with the results. Now, with your custom control, you must provide the same bindings there. By implementing `ControlValueAccessor` and providing `NG_VALUE_ACCESSOR`, you are telling Angular's Forms API how it can read and write values from/to your custom form control. - [Source](https://stackoverflow.com/questions/48085713/why-do-i-need-to-provide-ng-value-accessor-at-the-component-level)
295
+
296
+
`NG_VALUE_ACCESSOR` is just an injection token for ControlValueAccessor. You can refer the below one:
### The expanded provider configuration is an object literal with two properties:
303
+
304
+
- The `provide` property holds the token that serves as the key for consuming the dependency value.
305
+
- The second property is a provider definition object, which tells the injector how to create the dependency value. The provider-definition can be one of the following:
306
+
1. `useClass` - this option tells Angular DI to instantiate a provided class when a dependency is injected
307
+
2. `useExisting` - allows you to alias a token and reference any existing one.
308
+
3. `useFactory` - allows you to define a function that constructs a dependency.
309
+
4. `useValue` - provides a static value that should be used as a dependency.
310
+
311
+
## Sources
312
+
313
+
1. [How to PROPERLY implement ControlValueAccessor - Angular Form](https://blog.woodies11.dev/how-to-properly-implement-controlvalueaccessor/)
0 commit comments