angular - 如何以角度将值传递给其他对话框组件
问题描述
我有一个组件,我将它用作主组件上的对话框,它可以工作,但我必须知道如何向它发送一个值(一个对象),以便在用户触发对话框时将它的值插入我的表单(如果object = null 它使表格清晰);
这是我的组件
1-主要成分:
export class GestionDesChauffeursComponent implements OnInit {
constructor(private _fuseTranslationLoaderService: FuseTranslationLoaderService,
private service: GetionChauffeursService, private dialog: MatDialog) { this._fuseTranslationLoaderService.loadTranslations(english, turkish); }
Update_chauffeur(){
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.height = "570px";
dialogConfig.width = "550px";
this.dialog.open(EditNewChauffeursComponent,dialogConfig);
}
onEdit(row){
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = false;
dialogConfig.autoFocus = true;
dialogConfig.height = "570px";
dialogConfig.width = "550px";
//this.dialog.open(EditNewChauffeursComponent,dialogConfig);
this.dialog.open(EditNewChauffeursComponent,{
...dialogConfig,
data: {
// data I want to pass into the dialog
myVar: row
}
});
onDelete(key){
var index = index = this.listDriverElementEntity.map(x => {return x.key}).indexOf(key);
console.log(index);
this.listDriverElementEntity.splice(index, 1);
this.service.Removechauffeur(key).subscribe(data=>{
this.listdata = new MatTableDataSource(this.listDriverElementEntity);
console.log(data);
});
}
Rq:该行包含我的数据表的对象是我在触发 onEdit 函数时将发送的对象。
我的 EditNewChauffeursComponent 组件:
model: any = {};
profileForm = this.fb.group({
Nom_de_famille: ['', Validators.required],
Prénom: ['', Validators.required],
email: ['', [Validators.email,Validators.required]],
Code_personnel: ['', Validators.required],
N_tel: [''],
Département: ['', Validators.required]
});
constructor(public dialogRef: MatDialogRef<EditNewChauffeursComponent>, private fb: FormBuilder,private _snackBar: MatSnackBar,private service: GetionChauffeursService) { }
@Inject(MAT_DIALOG_DATA) indata: any
ngOnInit() {
console.log(this.indata);
}
setdefaultformvalues(row) {
this.profileForm.setValue({
Nom_de_famille: [row.FirstName],
Prénom: [row.LastName],
email: [row.Email],
Code_personnel: [row.DriverCode],
N_tel: [''],
Département: ['']
});
}
我只是更改了我的代码但是'indata'给出了未定义的值。
解决方案
你打开对话框可以传递数据:
例如:
// instead of :
this.dialog.open(EditNewChauffeursComponent,dialogConfig);
// you can do :
this.dialog.open(EditNewChauffeursComponent,{
...dialogConfig,
data: {
// data I want to pass into the dialog
myVar: 'my var val'
}
});
在模态方面,您必须添加一个注入器来接收数据,方法如下:
// ... my code
constructor(
// add the following to your constructor :
@Inject(MAT_DIALOG_DATA) indata: any ) { }
// you can use the injected variable as you would any other
// injected var, meaning something like:
ngOnInit() {
this.setdefaultformvalues(this.indata);//will have row objct
}
推荐阅读
- typescript - 打字稿联合类型泛型参数
- python-3.x - 在 pandas 数据框中创建一个新列,该列由数据框中每个玩家的前一年统计数据组成
- java - 为什么 JVM 没有在字符串池内存中“看到”重复的字符串值?
- core-bluetooth - 问:CoreBluetooth 外围设备:didConnect 调用了两次
- git - Git通过命令行压缩分支时,如何更新消息的日期
- c# - 从同一解决方案中的不同测试项目引用项目中的 C# 命名空间
- python - 有没有办法将 openpyxl 行(存储在列表中)写入新的 Excel 文件?
- c# - 从 DB2 视图中获取数据
- html - 返回顶部锚链接的偏移量
- javascript - 如何将逻辑和状态从主要组件拆分到多个文件