AngularMaterialdialog:是否有一种方法可以在模板中不使用“data.*”作为数据属性?
创始人
2024-10-28 09:01:30
0

可以使用MatDialogRef的injector来将数据注入到dialog中,然后在模板中使用它们。这样可以使模板中的数据属性更直接、更简洁。

以下是可以将数据注入到dialog中并在模板中引用它们的示例代码:

// Component that opens dialog import { Component } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { DialogComponent } from './dialog/dialog.component';

@Component({ selector: 'app-root', template: , }) export class AppComponent { constructor(private dialog: MatDialog) {}

openDialog(): void { const dialogRef = this.dialog.open(DialogComponent, { data: { name: 'John', age: 30 }, }); } }

// DialogComponent import { Component, Inject } from '@angular/core'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';

@Component({ selector: 'app-dialog', template:

Welcome {{ name }}

You are {{ age }} years old.

, }) export class DialogComponent { constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: { name: string; age: number } ) {}

get name(): string { return this.data.name; }

get age(): number { return this.data.age; } }

在DialogComponent中,使用MAT_DIALOG_DATA注入数据。然后,我们可以获取它们并将它们绑定到模板中的属性。

这样做可以使模板更简洁,更容易理解。

相关内容

热门资讯

科普攻略!epoker透视底牌... 科普攻略!epoker透视底牌,大菠萝789辅助器下载,大神讲解(有挂神器);致您一封信;亲爱大菠萝...
发现一款!wpk透视插件,we... 1、发现一款!wpk透视插件,wepoker轻量版透视系统,总结教程(有挂技巧);详细教程。2、we...
安装程序教程!wepoker有... 安装程序教程!wepoker有用吗,wepoker私人局辅助挂,教你教程(有挂方法),支持语音通讯、...
重大通报!wepoker有脚本... 重大通报!wepoker有脚本吗,wepoker私人局透视插件,2025新版(有挂攻略);玩家必备必...
一分钟了解!wepoker透视... 一分钟了解!wepoker透视脚本免费app,we-poker是什么软件,解密教程(有挂辅助)1、点...
关于!we-poker辅助软件... 《关于!we-poker辅助软件教程,智星德州插件,可靠教程(有挂辅助)》 we-poker辅助软件...
专业讨论!hhpoker开挂教... 专业讨论!hhpoker开挂教程,wpk透视插件,教你攻略(有挂透明)科技教程也叫必备教程,这是一款...
玩家必备科技!pokemmo脚... 1、玩家必备科技!pokemmo脚本辅助器下载,aapoker脚本怎么用,攻略方法(有挂攻略);详细...
分享实测!wepoker正确养... 分享实测!wepoker正确养号方法,wepoker有用吗,我来教教你(有挂辅助);1分钟了解详细教...
揭秘攻略!哈糖大菠萝能开挂吗,... 揭秘攻略!哈糖大菠萝能开挂吗,wepoker好友房开挂,透明挂教程(有挂教程);科技安装教程;136...