Using MdDialogConfig data on Angular 2

前端 未结 3 1918
深忆病人
深忆病人 2021-01-13 10:37

I\'m trying to use a dialog component in Angular 2 using @angular/material2.0.0-beta.1. What I\'m trying to accomplish is to send data (which are values that a

3条回答
  •  渐次进展
    2021-01-13 10:51

    Update: Definitely use the above Inject method to get your data. This way is dependent upon accessing a private member which is not guaranteed to remain untouched between versions

    If you're using the new 2.0.0 beta 3 (or maybe it was even changed in beta 2, not sure) then the above answer has to be changed a little bit:

    @Component({
    selector: "dialog",
    templateUrl: "./dialog.component.html"
    })
    export class DialogComponent {
        constructor(public dialogRef: MdDialogRef) { }
        type: string;
        name: string;
        ngOnInit(): void {
            let configData: any = this.dialogRef._containerInstance.dialogConfig.data;
            this.type = configData.type;
            this.name = configData.name;
       }
    }
    

    It seems like there should be a better way of referencing the data than this.dialogRef._containerInstance.dialogConfig.data but I couldn't find one

提交回复
热议问题