ngx-mat-select-search未设置默认值

时间:2019-12-20 08:19:15

标签: angular angular-material ngx-bootstrap

我正在使用具有ngx-mat-select-search的选择组件,我想设置调用该组件时选择的默认值,但是它没有显示并且看不到任何内容控制台错误。我不知道发生了什么,我想念什么?

这是StackBlitz

https://stackblitz.com/edit/angular-hre1qd

这也是我用来设置默认值的主要代码:

test.component.html

<mat-form-field [formGroup]="formData" class="prueba">
    <mat-select formControlName="valueSelect"  #singleSelect>
        <mat-option>
            <ngx-mat-select-search [formControl]="inputFilter" [placeholderLabel]="'Search...'"
                 [noEntriesFoundLabel]="'Not Found...'">
                <i class="mdi mdi-close menu-icon" ngxMatSelectSearchClear></i>
            </ngx-mat-select-search>
        </mat-option>
        <mat-option value='-1' selected>{{defaultValue}}</mat-option>
        <mat-option *ngFor="let data of controlFilter | async" [value]="data[dataValue]">
            {{ data[dataShow] }}
        </mat-option>
    </mat-select>
</mat-form-field>

test.component.ts

setInitialValue() {
    this.controlFilter.next(this.dataSource.slice());
    this.formData
      .get("valueSelect")
      .setValue(this.dataSource[2]);
    // WHY IS NOT CHANGING TO A DEFAULT VALUE
  }

感谢劝告!

2 个答案:

答案 0 :(得分:1)

那是因为您只需要传递view_action_bar.itemIconTintList=null 而不是整个对象:

code

Forked Stackblitz

答案 1 :(得分:1)

您要将整个对象分配给'valueSelect',要自动填充选项,只需传递引用即可,这里的代码是唯一可以使用的代码,默认值将被自动填充

尝试一下

  setInitialValue() {
    this.controlFilter.next(this.dataSource.slice());
    this.formData
      .get("valueSelect")
      .setValue(this.dataSource[2].code); //here based on formControl value that having same code; that obj name field will be displayed/populated in selected option
  }

如果不需要,您可以使用<mat-option value='-1' selected>{{defaultValue}}</mat-option>显示/选择空选项。

相关问题