问题描述: 在使用Angular Material底部弹出窗口时,尝试将变量作为数据传递给弹出窗口组件时出现问题。
解决方法:
示例代码如下:
dialog.service.ts:
import { Injectable } from '@angular/core';
@Injectable()
export class DialogService {
private dialogData: any;
constructor() { }
getDialogData() {
return this.dialogData;
}
setDialogData(data: any) {
this.dialogData = data;
}
}
component1.ts:
import { Component } from '@angular/core';
import { DialogService } from './dialog.service';
@Component({
selector: 'app-component1',
template: `
`,
})
export class Component1 {
constructor(private dialogService: DialogService) { }
openDialog() {
const data = { name: 'John', age: 30 };
this.dialogService.setDialogData(data);
// 打开弹出窗口
}
}
dialog.component.ts:
import { Component } from '@angular/core';
import { DialogService } from './dialog.service';
@Component({
selector: 'app-dialog',
template: `
{{ dialogService.getDialogData().name }}
{{ dialogService.getDialogData().age }}
`,
})
export class DialogComponent {
constructor(private dialogService: DialogService) { }
}
示例代码如下:
component1.ts:
import { Component } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { DialogComponent } from './dialog.component';
@Component({
selector: 'app-component1',
template: `
`,
})
export class Component1 {
constructor(private dialog: MatDialog) { }
openDialog() {
const dialogRef = this.dialog.open(DialogComponent, {
data: { name: 'John', age: 30 }
});
}
}
dialog.component.ts:
import { Component, Inject } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
@Component({
selector: 'app-dialog',
template: `
{{ data.name }}
{{ data.age }}
`,
})
export class DialogComponent {
constructor(@Inject(MAT_DIALOG_DATA) public data: any) { }
}
这些解决方法可以帮助你将变量作为数据传递给Angular Material底部弹出窗口组件。根据你的需求选择其中一种方法来实现。
上一篇:Angular Material的“Bootstrap sr-only equivalent”等价写法
在Angular Material中,等价于“Bootstrap sr-only”的写法