angular - 将组件数据共享到 Angular Material 对话框
问题描述
我想在打开对话框时显示特定的设备数据,但首先,我需要将device.key
组件从我的组件传递给对话框组件,然后运行将获取设备数据的服务,让我向您展示代码:
列表设备.html:
<ng-container matColumnDef="info">
<th mat-header-cell *matHeaderCellDef><strong>Details</strong></th>
<td mat-cell *matCellDef="let device; let i = index;">
<button mat-mini-fab (click)="openDialog(device.key)" style="background-color: darkcyan;">
<mat-icon>info</mat-icon>
</button>
</td>
</ng-container>
列表设备.ts:
openDialog(id: string){
this.dialog.open(ListDialogComponent)
console.log(id)
}
列表设备dialog.ts:
dataSource: any = [];
constructor(
public fb: FormBuilder,
private location: Location,
private deviceService: DeviceService,
private actRoute: ActivatedRoute,
private router: Router
) {
this.deviceService.GetDevice(**the device key**).valueChanges().subscribe(data => {
console.log(data);
this.dataSource.push(data)
})
}
解决方案
openDialog(id: string){
this.dialog.open(ListDialogComponent, {
data: {id: id}
});
}
在对话框组件中
导入库
import {Component, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
然后将其注入构造函数中,
constructor(
public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: DialogData) {}
在这里你可以看到 => this.data 的值
推荐阅读
- ruby-on-rails - AWS 的 MongoDB 性能问题
- java - bartMachine 在重复执行时会增加内存使用量
- typescript - 防止组合对象中的重复键,{ [K in keyof T]: T[K] } 的反转
- reactjs - 占位符未显示在下拉列表中,有人可以告诉我为什么吗?
- php - 如何在 PHP 中修复“未闭合元素 a”
- html - 在 div 与 h1 上设置字体大小的区别
- symfony - 重定向防火墙 symfony 4
- python - 电影游戏 - 在电影开头跳过“The”
- amazon-s3 - S3 存储桶的 VPC 访问策略
- java - Spring Data Elasticsearch 什么时候支持服务器版本 7.1?