24

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)
  });
}
1

3 Answers 3

20

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)
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah great! I thought it may be something similar, but wasn't sure how to go about it. I just had a Google of the difference between setValue() and patchValue() and opted for patch for now.
2

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

Comments

0

Why would you define the form control within the subscribe? My advice would be to structure the form skeleton outside the subscription and just populate the control using

mySubscription() {
  this.myService.myFunct.subscribe(res => {
    this.controls['select'].patchValue(res.status);
  });
}

1 Comment

where does the controls come into things?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.