search

Home  >  Q&A  >  body text

Angular dropdown not reading value

I'm new to Angular, so please forgive me if I make any mistakes. I'm trying to get a created list in a dropdown and when the user selects it it should log the information into the database.

This is my code: Component.html

<mat-form-field appearance="fill">
  <mat-label>Retrieval Reason</mat-label>
  <mat-select [formControl]="RR" required>
    <mat-option>--</mat-option>
    <mat-option *ngFor="let reason of reasons" [value]="reason">
      {{reason.reason}}
    </mat-option>
  </mat-select>
  <mat-error *ngIf="RR.hasError('required')">Please choose a reason</mat-error>
</mat-form-field>

  <button
    mat-raised-button
    (click)="done()"
    color="primary"
    [disabled]="selection.selected.length === 0 || RR.hasError('required')"
  >
    Retrieve
  </button>

Component.ts

retrievalReason: Reasons;
    RR = new FormControl('', Validators.required);

  reasons: Reasons[] = [
    {reason: 'Cycle Count'},
    {reason: 'Purge Request'},
    {reason: 'Picking'},];

    done() {
    this.dialogRef.close({
      carrier: this.carrier,
      destination: this.selection.selected[0].dstId,
      retrievalReason: this.RR.get('reasons').value,
    });
  }

I looked up Angular methods for reading values ​​from dropdowns and tried different variable names but nothing has worked so far.

P粉513316221P粉513316221272 days ago302

reply all(1)I'll reply

  • P粉022723606

    P粉0227236062024-02-22 17:14:16

    I think the only thing that might be wrong is if you try retrievalReason: this.RR.get('reasons').value but that's to get the form control of the form.

    You only have one form control, so just retrievalReason:this.RR.value is enough.

    reply
    0
  • Cancelreply