【问题标题】:How to send data in Material 2 dialog Angular 2 from 1 component to other如何在材料 2 对话框 Angular 2 中将数据从 1 个组件发送到其他组件
【发布时间】:2017-06-20 12:46:01
【问题描述】:

我正在使用 Material 2 对话框,我能够在对话框关闭时取回数据。

但我找不到任何解决方案来与@Input 对话发送数据

import {Component} from '@angular/core';
import {MdDialog, MdDialogRef} from '@angular/material';

@Component({
  selector: 'dialog-result-example',
  templateUrl: './dialog-result-example.html',
})
export class DialogResultExample {
  selectedOption: string;

  constructor(public dialog: MdDialog) {}

  openDialog() {
    let dialogRef = this.dialog.open(DialogResultExampleDialog);
    dialogRef.afterClosed().subscribe(result => {
      this.selectedOption = result;
    });
  }
}

@Component({
  selector: 'dialog-result-example-dialog',
  templateUrl: './dialog-result-example-dialog.html',
})
export class DialogResultExampleDialog {
  constructor(public dialogRef: MdDialogRef<DialogResultExampleDialog>) {}
}

【问题讨论】:

    标签: angular angular-material2


    【解决方案1】:

    您可以使用MdDialogRefcomponentInstance 属性,正如yurzui 在此question 答案的第8 步 中所建议的那样。

    例如,如果您想将值 foo 传递给 DialogResultExampleDialog 中的变量 param1,您可以执行以下操作:

    import {Component} from '@angular/core';
    import {MdDialog, MdDialogRef} from '@angular/material';
    
    @Component({
      selector: 'dialog-result-example',
      templateUrl: './dialog-result-example.html',
    })
    export class DialogResultExample {
      selectedOption: string;
    
      constructor(public dialog: MdDialog) {}
    
      openDialog() {
        let dialogRef = this.dialog.open(DialogResultExampleDialog);
        dialogRef.componentInstance.param1 = "foo";
        dialogRef.afterClosed().subscribe(result => {
          this.selectedOption = result;
        });
      }
    }
    
    
    @Component({
      selector: 'dialog-result-example-dialog',
      templateUrl: './dialog-result-example-dialog.html',
    })
      param1: string;
    
      export class DialogResultExampleDialog {
      constructor(public dialogRef: MdDialogRef<DialogResultExampleDialog>) {}
    }
    

    【讨论】:

    • 我认为 param1 进入了 DialogResultExampleDialog 的主体,对吧?
    【解决方案2】:

    另一种方式,你可以使用MdDialogConfig

    import {Component} from '@angular/core';
    import {MdDialog, MdDialogRef, MdDialogConfig} from '@angular/material';
    
    
    @Component({
      selector: 'dialog-result-example',
      templateUrl: './dialog-result-example.html',
    })
    export class DialogResultExample {
      selectedOption: string;
    
      constructor(public dialog: MdDialog) {}
    
      openDialog() {
        let config = new MdDialogConfig;
        if (id) {
          config.data = { id: id }
        } else config.data = null;
    
        let dialogRef = this.dialog.open(DialogResultExampleDialog, config);
        dialogRef.afterClosed().subscribe(result => {
          this.selectedOption = result;
        });
      }
    }
    
    
    @Component({
      selector: 'dialog-result-example-dialog',
      templateUrl: './dialog-result-example-dialog.html',
    })
    export class DialogResultExampleDialog {
      constructor(public dialogRef: MdDialogRef<DialogResultExampleDialog>) {
        if (dialogRef.config.data) {
                // do something...
         }
      }
    }
    

    【讨论】:

    • 这看起来是一种更好的方法(因为数据可以在Dialog的构造函数中配置)。谢谢!
    猜你喜欢
    • 2019-06-11
    • 2019-05-14
    • 2019-06-12
    • 2018-07-07
    • 2018-01-16
    • 2017-10-19
    • 1970-01-01
    • 2017-05-02
    • 2019-01-19
    相关资源
    最近更新 更多