Angular下拉列表无法读取值。

huangapple go评论64阅读模式
英文:

Angular Dropdown list not reading value

问题

<!-- 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.value, // Changed this line
  });
}

I've made some adjustments to your code, specifically changing the way you retrieve the selected value from the dropdown list in the Component.ts file. The change should address your issue with reading the value from the dropdown list in Angular.

英文:

I'm new to Angular so forgive me if I have this whole thing wrong. I'm attempting to get a created list in a dropdown and when the user selects it, it should record the information to the database.

Here is my code:
Component.html

&lt;mat-form-field appearance=&quot;fill&quot;&gt;
  &lt;mat-label&gt;Retrieval Reason&lt;/mat-label&gt;
  &lt;mat-select [formControl]=&quot;RR&quot; required&gt;
    &lt;mat-option&gt;--&lt;/mat-option&gt;
    &lt;mat-option *ngFor=&quot;let reason of reasons&quot; [value]=&quot;reason&quot;&gt;
      {{reason.reason}}
    &lt;/mat-option&gt;
  &lt;/mat-select&gt;
  &lt;mat-error *ngIf=&quot;RR.hasError(&#39;required&#39;)&quot;&gt;Please choose a reason&lt;/mat-error&gt;
&lt;/mat-form-field&gt;

  &lt;button
    mat-raised-button
    (click)=&quot;done()&quot;
    color=&quot;primary&quot;
    [disabled]=&quot;selection.selected.length === 0 || RR.hasError(&#39;required&#39;)&quot;
  &gt;
    Retrieve
  &lt;/button&gt;

Component.ts

  retrievalReason: Reasons;
    RR = new FormControl(&#39;&#39;, Validators.required);

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

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


I've looked up the Angular method to reading a value from a dropdown list and have tried different variable names, nothing's worked so far.

答案1

得分: 1

我看到可能有问题的地方只是你尝试使用retrievalReason: this.RR.get('reasons').value,但这是用于获取表单控件的。你只有一个表单控件,所以只需要retrievalReason: this.RR.value就足够了。

英文:

The only thing I see that could be wrong is that you try retrievalReason: this.RR.get(&#39;reasons&#39;).value but this is for a form to get the formcontrol.

You only have a formcontrol so just retrievalReason:this.RR.value should be enough.

huangapple
  • 本文由 发表于 2023年2月8日 20:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定