简体   繁体   English

如何使用 MatDialog 将行数据或数组对象传递给 Angular 中的组件

[英]how to pass a row data or the object of an array to a component in Angular using MatDialog

What I want is that, when I click on the specific row's button, it should open up a matDialog box which should display me all the contents of the row.我想要的是,当我单击特定行的按钮时,它应该打开一个 matDialog 框,该框应该显示该行的所有内容。 This is the html file ,这是html文件

                           <tr *ngFor="let u of users">
                                <td data-label="ID">{{u.name}}</td>
                                <td>
                                    <button (click)="toggle(u)" mat-button type="button" class="btn btn-primary">
                                        Show Details
                                    </button>
                                </td>
                            </tr>

And this is my ts file .这是我的ts 文件


users = [
    {
      'name':'arjun'
    },
    {
      'name':'Karan'
    }
  ]


toggle(userRow:any){
    const dialogConfig = new MatDialogConfig();
    dialogConfig.disableClose = false;
    dialogConfig.autoFocus = true;
    dialogConfig.width="60%";
    this.dialog.open(UserDetailsPopupComponent, dialogConfig);
  }

And this is my html file of the component( UserDetailsPopUp ) that will open using MatDialogue.这是我将使用 MatDialogue 打开的组件( UserDetailsPopUp )的html 文件

<h1>Hello{{u.name}}</h1>

But I'm not able to send the row data from the toggle function to this component.但我无法将行数据从切换功能发送到此组件。 How do I do that?我怎么做? And load that data and display it.并加载该数据并显示它。

Send data to the dialog component like this:像这样将数据发送到对话框组件:

toggle(userRow:any){
    const dialogConfig = new MatDialogConfig();
    dialogConfig.disableClose = false;
    dialogConfig.autoFocus = true;
    dialogConfig.width="60%";
    dialogConfig.data = userRow;
    this.dialog.open(UserDetailsPopupComponent, dialogConfig);
  }

and to catch the data in UserDetailsPopupComponent并捕获UserDetailsPopupComponent的数据

constructor(@Inject(MAT_DIALOG_DATA) public _data: any) {}

_data contains the userRow data. _data包含userRow数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 6 - MatDialog - EventEmitter - 从 MatDialog 将对象共享给父组件 - Angular 6 - MatDialog - EventEmitter - share object to parent component from MatDialog 如何将 BehaviorSubject 传递给 MatDialog 数据? - How can i pass BehaviorSubject to MatDialog data? 如何使用 Angular EventEmitter 将数据从一个组件传递到另一个组件 - How to pass data from on component to another using Angular EventEmitter 如何按值将对象传递给 Angular2+ 组件 - How to pass object by value into Angular2+ component 使用 Input 组件的 Angular Pass 数据 - Angular Pass data using Input component 如何将数据传递给 Angular 中另一个组件中的表单 - how to pass data to a form in another component in Angular 如何访问插槽 object 属性以传递给 Vuetify 中给定数据表行的组件? - How to access slot object property to pass into component for a given data table row in Vuetify? 在Angular中,如何将JSON对象/数组传递给指令? - In Angular, how to pass JSON object/array into directive? 如何将对象数组从Node.js传递到angular - How to pass array of object from nodejs to angular 使用Angular将文本框数据传递到Javascript数组 - Pass Textbox Data To A Javascript Array Using Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM