angular - 将 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
?
谢谢你
解决方案
推荐阅读
- python - 难以将 xticks 对齐到直方图 bin 的边缘
- python - 添加图像时,Python Tkinter 按钮不再单击
- android - Dagger2 依赖循环使用@Binds 和@Inject 字段
- string - 我的指数超出范围,我开始恐慌,我无法摆脱
- accessibility - 屏幕阅读器应该宣布短格式标签还是长格式值?
- python - 使用 scipy 组合 p 值
- android-studio - 我应该更新 android studio 4.0 吗?
- r - 如何在保留配对的同时按第二列对 R double 进行排序?
- azure-eventhub - Azure 事件中心使用不记名令牌发送消息
- python-3.x - 在 jupyter 中挑选列表时遇到问题