Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use [formControl] without also needing [formGroup]?

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)
  });
}
like image 395
physicsboy Avatar asked Sep 14 '18 10:09

physicsboy


People also ask

Can I use FormControl without FormGroup?

Solution. Yes, you can use FormControlDirective without FormGroup.

Can I use form control without form?

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.

What is the difference between FormGroup and FormControl?

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.

Can we use FormControl and formControlName together?

FormControlNamelinkSyncs a FormControl in an existing FormGroup to a form control element by name.


2 Answers

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)
  });
}
like image 136
yurzui Avatar answered Oct 11 '22 23:10

yurzui


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
like image 41
Ganesh K Avatar answered Oct 11 '22 22:10

Ganesh K