javascript - Mat-select 不显示所选值

标签 javascript typescript angular6 angular-material-6 formgroups

This is what I get当我的编辑表单加载时,我想用初始值填充表单数组。 我的下拉选择选项被禁用,但它没有显示在垫选择中。

let selectedSpecs = this.editData.element.specifications;

this.spec = this.specForm.get('spec') as FormArray;

if (selectedSpecs.length != 0){
  let selectedAttributeIds = [];
  selectedSpecs.forEach((spec, i) => {
    let attribute;
    attribute = {'id': spec.itemDetailId, 'itemId':this.editData.element.itemId, 'name': spec.name};

    this.spec.push(this.formBuilder.group({
      attribute: attribute,
      attributeSpec: spec.description
    }));

    selectedAttributeIds.push(spec.itemDetailId);

  });

  let attributes = [];
  this.selectedCatAttributes.forEach((tempattribute) => {
    let selected;
    if (selectedAttributeIds.includes(tempattribute.id)) {
      selected = true;
    }else {
      selected = false;
    }
    attributes.push(new Attribute(tempattribute, !selected));
  });

  this.attributeList.next(attributes);


}





<div formArrayName="spec" *ngFor="let spec of specForm.get('spec')['controls']; let i= index">
      <div [formGroupName]="i">
        <mat-card class="addShadow">
          <button style="float: right" mat-icon-button (click)="removeSpec(i)"><mat-icon class="specClear">cancel</mat-icon></button>


          <mat-form-field class="spec">
            <mat-select placeholder="Select Attribute" formControlName="attribute" (selectionChange)="selectedOption($event.value,i)">
              <mat-option *ngFor="let attribute of attributeList | async; let index = index" [value]="attribute.attribute" [disabled]="!attribute.allowed">
                {{attribute.attribute.name}}
              </mat-option>
            </mat-select>
          </mat-form-field>

          <mat-form-field class="spec">
            <input matInput placeholder="Specification" formControlName="attributeSpec">
          </mat-form-field>
        </mat-card>
      </div>
    </div>

如何在下拉列表中显示初始值。我正在使用表单数组。我尝试过使用 [value] 以及 [compareWith],但这些都不适合我。

最佳答案

我尝试了很多次,终于找到了解决这个问题的方法。我使用[compareWith]来解决这个问题。这是我的解决方案。

这是我在 [compareWith] 中调用的方法。

attributeDisplay(attribute1,attribute2){
if (attribute1.id == attribute2.id) {
  return attribute1.name;
} else {
  return "";
}

}

这是我的 html。

 <mat-form-field class="spec">
            <mat-select placeholder="Select Attribute" formControlName="attribute" (selectionChange)="selectedOption($event.value,i)" [compareWith]="attributeDisplay">
              <mat-option *ngFor="let attribute of attributeList | async; let index = index" [value]="attribute.attribute" [disabled]="!attribute.allowed">
                {{attribute.attribute.name}}
              </mat-option>
            </mat-select>
          </mat-form-field>

关于javascript - Mat-select 不显示所选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58132680/

相关文章:

angular - 修补项目不会更新 Angular 中的有效性

javascript - 将一些库导入 Angular

angular - 如何使用 angular 2 将表单数据发送到服务器 api?

javascript - 单击事件的父/子元素绑定(bind)

typescript - TypeWriter - 过滤没有给定属性的类或属性

node.js - Sequelize : instance. get 不是函数(升级后)

javascript - Angular 6如何从YTS API获取数据

angular - angular 6 中 ng add <package name> 与 npm install <package name> 之间的区别

javascript - 我可以通过 Javascript 向表单添加参数吗?

javascript - 添加 cookie 仅显示一次弹出窗口