How can I pass a service variable into an Angular Material dialog?

前端 未结 5 1618
眼角桃花
眼角桃花 2020-12-24 10:40

For mdDialog, how do I pass in variable? Specifically, how to inject an Angular service into the dialog component?

5条回答
  •  孤独总比滥情好
    2020-12-24 11:29

    From the official docs found on https://material.angular.io/components/dialog/overview

    Sharing data with the Dialog component.

    If you want to share data with your dialog, you can use the data option to pass information to the dialog component.

    let dialogRef = dialog.open(YourDialog, {
      data: 'your data',
    });
    

    To access the data in your dialog component, you have to use the MD_DIALOG_DATA injection token:

    import {Component, Inject} from '@angular/core';
    import {MD_DIALOG_DATA} from '@angular/material';
    
    @Component({
      selector: 'your-dialog',
      template: 'passed in {{ data }}',
    })
    
    export class YourDialog {
      constructor(@Inject(MD_DIALOG_DATA) public data: any) { }
    }
    

提交回复
热议问题