我是 Angular 的新手,所以如果我有任何错误,请原谅我。我正在尝试在下拉列表中获取创建的列表,当用户选择它时,它应该将信息记录到数据库中。
这是我的代码: 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,
});
}
我查找了从下拉列表中读取值的 Angular 方法,并尝试了不同的变量名称,但到目前为止没有任何效果。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我认为唯一可能错误的是您尝试
retrievalReason: this.RR.get('reasons').value但这是为了获取表单控件的表单。您只有一个表单控件,因此只需
retrievalReason:this.RR.value就足够了。