【问题标题】:Unable to set default value for dropdown in Angular Material无法为 Angular Material 中的下拉菜单设置默认值
【发布时间】:2021-04-13 18:07:56
【问题描述】:

我在 Angular Material 中使用mat-select,如下所示:

<mat-form-field class="full-width">
   <mat-select (selectionChange)="dropdownSelectedItem($event)">
      <mat-option *ngFor="let item of itemList" [value]="item">
      {{item.name}}
      </mat-option>
   </mat-select>
</mat-form-field>

上面的下拉菜单很好,但下拉菜单没有默认值。

为了添加默认值,我添加了value="{{dropdownSelectedName}}",如下所示:

<mat-form-field class="full-width">
   <mat-select (selectionChange)="dropdownSelectedItem($event)" value="{{dropdownSelectedName}}">
      <mat-option *ngFor="let item of itemList" [value]="item">
      {{item.name}}
      </mat-option>
   </mat-select>
</mat-form-field>

以上更改仍未填充默认下拉值。

我也试过[(value)]="dropdownSelectedName"

当我在组件中做console.log(this.dropdownSelectedName)时,我可以看到它有一个字符串值 mydefaultvalue

ngOnInit() {
    this.subs.sink = this.myService.listAll()
        .subscribe(r => {
            this.AllList = r;
        });
    this.dropdownSelectedName = "mydefaultvalue";
}

【问题讨论】:

    标签: html angular typescript angular-material dropdown


    【解决方案1】:
    • mat-option 的值是对 item 对象的对象引用。
    • 因此,您在mat-select 上设置的值必须与item 对象引用完全相同。

    这意味着在你的组件中你的代码应该

    this.dropdownSelectedName = [Insert Object Reference Here]
    

    另一种方法是将您的mat-option 更改为设置

    <mat-option *ngFor="let item of itemList" [value]="item.name">
      {{item.name}}
    <mat-option>
    

    通过此更改,您的“myDefaultValue”将匹配其中一个选项的值,而不是对象引用

    更新

    Here 是一个链接,显示了在使用对象引用时设置选择值的工作版本。

    【讨论】:

    • 但这并不能回答我的问题。当用户第一次打开它时,我想将下拉列表的值填充到字符串mydefaultvalue。此值不同于 itemList 中的值
    • 因为您将mat-option 值设置为对象引用。您的 dropdownSelectedName 需要将该值设置为相同的对象引用。一旦你这样做,角度将找到具有匹配对象引用的 mat-option。在我的回复中,状态为[Insert Object Reference Here] 的行在哪里。而不是传递一个字符串。找到要设置为默认值的item 对象引用,并将其用作默认值。而不是字符串
    • 当我执行this.dropdownSelectedName = [Insert Object Reference Here] 时,它仍然没有将默认值填充到mydefaultvalue
    • 我已经用 stackblitz 中工作版本的链接更新了答案
    【解决方案2】:

    要将默认值设置为mat-select,您必须绑定compareWith 属性,该属性将比较值并设置默认值。

    例子:

    在您的 HTML 中:

    <mat-form-field class="full-width">
       <mat-select (selectionChange)="dropdownSelectedItem($event)" [(ngModel)]="dropdownSelectedName" [compareWith]="compareFn">
          <mat-option *ngFor="let item of itemList" [value]="item">
          {{item.name}}
          </mat-option>
       </mat-select>
    </mat-form-field>
    

    在您的 TS 中:

    compareFn(obj1: any, obj2: any) {
        return obj1 && obj2 ? obj1.id === obj2.id : obj1 === obj2;
    }
    

    【讨论】:

    • 它工作正常,完全符合预期。谢谢。你能帮我理解return obj1 &amp;&amp; obj2 ? obj1.id === obj2.id : obj1 === obj2在做什么吗?在我的情况下它总是返回false
    • 基本上,当我们尝试为mat-select设置一些预定义的值时,由于一些引用问题,它不会直接起作用,因此无法设置该值。 compareFn 告诉 Angular 如何比较值的函数。看看当你调试compareFn时,它会检查项目列表为obj1,你的值为obj2,它会一直运行直到条件成立。如果找到匹配项,它将设置该值。
    猜你喜欢
    • 2019-12-28
    • 1970-01-01
    • 2021-08-15
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2018-09-29
    相关资源
    最近更新 更多