从下拉列表中选择其他值后,mat-select mat-option 角度下拉列表不会更改值

时间:2021-05-18 21:55:04

标签: angular dropdown mat-table

enter image description here enter image description here

enter image description here enter image description here

我使用的是Angular材质表,mat-select和mat-option下拉菜单,对于Media下拉菜单,值为Email,lk_type为3287,选择phone后,media值为phone,lk_type为3286 ,但是,当我单击编辑按钮时,this.currentData 没有更改下拉值,它仍然保留旧值,您知道我如何更改此 angular mat-select 和 mat-option 下拉值的下拉值吗? ?

add-person.component.html

<mat-table [dataSource]="dataSource">
  <ng-container matColumnDef="person_contact_ID">
    <mat-header-cell *matHeaderCellDef> Person Contact ID </mat-header-cell>
    <mat-cell *matCellDef="let row">
      <mat-form-field floatLabel="{{ row.editing ? 'float' : 'never'}}">
        <input [(ngModel)]="row.currentData.person_contact_ID" placeholder="Person Contact ID" [disabled]="!row.editing" matInput>
      </mat-form-field>
    </mat-cell>
  </ng-container>
<ng-container matColumnDef="media">
    <mat-header-cell *matHeaderCellDef> Media </mat-header-cell>
    <mat-cell *matCellDef="let row">
      <mat-form-field>
        <mat-select style="min-width: 200px;" placeholder="" value="{{row.currentData.lk_type}}">
          <mat-option *ngFor="let media of mediaList " value="{{media.lookup_id}}">
            {{ media.descr }}
          </mat-option>
        </mat-select>
      </mat-form-field>
    </mat-cell>
  </ng-container>
  

<ng-container matColumnDef="actionsColumn">
    <mat-cell *matCellDef="let row">
      <button *ngIf="row.editing" mat-icon-button color="primary" focusable="false" (click)="row.confirmEditCreate_AddPerson()">
        <i class="fa fa-check mat-icon"></i>
      </button>
    </mat-cell>
  </ng-container>
<mat-header-row *matHeaderRowDef="displayedColumn
s"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

tabledata.ts

confirmEditCreate_AddPerson(): boolean {
    //_personService: TestRDB2Service;
    if (this.id == -1)
      return this.source.confirmCreate(this);
    else
      
      try {
        this._http.put("https://localhost:5001/" + 'api/TestRDB2/AddUpdatePerson_Contact/', this.currentData).subscribe();        
      } catch (e) {
        return e
      }
    return true;
      //return this.source.confirmEdit(this);
  }

1 个答案:

答案 0 :(得分:1)

如果您在 mat-table 中使用 mat-select,则需要使用双向绑定 ngModel 以便在 currentData 对象中设置数据,请查看以下代码以供参考:

>>> [sl[-1] for sl in (line.split() for line in txt.splitlines()) if sl[0]=='TP1']
['156.2', '30']