I am using a dropdown in my UI, but when I come to use [formControl]
I am getting the error thrown:
Cannot find control with unspecified name attribute
I am using ReactiveFormsModule in my app.module.ts
.
I have Google'd and found that a solution is to use [formGroup] in the parent div
, but I'm unsure of how to implement properly as I am defining my formControl
from within a subscribe
.
myComp.component.html
<div class="exceptional-status">
<select #exceptionalSelect id="excep-status-dropdown" [formControl]="select">
<option disabled value="default">--Exceptional statuses--</option>
<!-- other options here with *ngFor -->
</select>
</div>
myComp.component.ts
select: FormControl;
mySubscription() {
this.myService.myFunct.subscribe(res => {
this.select = new FormControl(res.status)
});
}
Solution. Yes, you can use FormControlDirective without FormGroup.
Angular FormControl is an inbuilt class used to get and set values and validate the form control fields like <input> or <select>. The FormControl tracks the value and validation status of an individual form control. It can be used standalone as well as with a parent form.
FormControl and FormGroup in AngularFormGroup is used with FormControl to track the value and validate the state of form control. In practice, FormGroup aggregates the values of each child FormControl into a single object, using each control name as the key.
FormControlNamelinkSyncs a FormControl in an existing FormGroup to a form control element by name.
Yes, you can use FormControlDirective
without FormGroup.
Angular expects FormControl to be defined:
select = new FormControl();
mySubscription() {
this.myService.myFunct.subscribe(res => {
this.select.setValue(res.status)
});
}
Yes, you can use it without FormGroup.
select = new FormControl();
For set the value:-
select.setValue('your data here');
To get the value:-
select.value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With