【问题标题】:Show default selected option in mat-select in Angular在 Angular 的 mat-select 中显示默认选择的选项
【发布时间】:2021-07-22 04:08:45
【问题描述】:

我在 Angular 11 中使用 mat select。任何人都可以帮我在 mat-select 中显示选定的选项。 我有一个名为 Idefault 的字段,条件是, 如果 type.Idefault == true 然后在 mat-select 中将值显示为选定选项

添加图片供您参考

             <div class="col-sm-6 has-select pr-3">
                  <mat-form-field appearance="fill" class="pt-1 no-border">
                    <mat-select formControlName="source"  [(ngModel)]="selectedBatchSource" (selectionChange)="changeClient($event.value)" >
                      <mat-option *ngFor="let type of data$ | async"  [value]="type.ctype">
                        {{type.ctype}}
                      </mat-option>
                    </mat-select>
                  </mat-form-field>
                </div>

【问题讨论】:

  • 我对material api不太熟悉,但是你不会只是在你的组件中预先定义this.selectedBatchSource来分配默认值吗?
  • 感谢您的回复,但在这里我想在打开表单时显示选定的值。当用户从列表中选择选项时,我使用 selectedBatchSource 从列表中获取所选值。

标签: angular typescript angular-material


【解决方案1】:

 this.heroForm = new FormGroup({
    source: new FormControl('your default value here')
     
    ]),
使用反应形式我认为如果你这样做会有所帮助

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多