首页 > 解决方案 > 将 db 中的选定值放入 mat 选项 angular 12

问题描述

我想将已保存在数据库中的添加操作中的选定值放入mat-select或显示在编辑操作中mat-option。这mat-select也用于添加操作。在添加操作中,该值为空,而在编辑操作中,应显示来自 db 的值。所选值是表 A 中的外键,它是表 B 中的主键

我的 .html 文件在下面

                <mat-form-field  appearance="outline" > 
                    <mat-select placeholder="Pilih Ruang " [(value)]="selected1" name="NAMA_RUANG" [(ngModel)]="dataKamar.ID_RUANG">
                        <mat-option *ngFor="let row of dataRuang" [value]="row.KODE_RUANG">{{row.NAMA_RUANG}}</mat-option>
                    </mat-select>
                </mat-form-field>

我的 .ts 文件在下面调用 dataRuang

  getRuang(){
    this.api_ruang.baca().subscribe((result: any)=> {
      this.dataRuang = result;
    })
  }

调用 dataKamar

  getAll(){
    this.api_kamar.getList().subscribe((result: any) => {
      this.dataSource = new MatTableDataSource(result)
      this.dataSource.paginator = this.paginator
      this.dataSource.sort = this.sort
    })  

  }  

保存

  addItem(){
    this.dataKamar.ID_RUANG = this.selected1;
    this.api_kamar.addItem(this.dataKamar).subscribe((response: any) => {
      this.getAll()
      this.cancelEdit()     
    })
  }

编辑

  editItem(element: any){
    this.dataKamar = _.cloneDeep(element)
    this.isEdit = true
  }


  updateItem(){
    this.api_kamar.updateItem(this.dataKamar.ID_KAMAR, this.dataKamar).subscribe((response: any) => {
      this.getAll()
      this.cancelEdit()      
    })
  }

我如何将选定的值从数据库中放入mat-option

谢谢你

标签: angularangular12mat-select

解决方案


推荐阅读