简体   繁体   English

通过触发对话框叠加功能在角度组件之间传递值

[英]Passing Value Between Angular Components via a Function for Triggering Dialog Overlay

In my Angular app, I have a component with a function that opens a dialog overlay. 在我的Angular应用程序中,我有一个带有打开对话框叠加功能的组件。 I am trying to figure out how to pass some data from the originating component to this dialog component (EnrollingProcessComponent). 我试图弄清楚如何将一些数据从原始组件传递到此对话框组件(EnrollingProcessComponent)。 This is not a parent-child relationship, so I can't use Input() or [] binding here. 这不是父子关系,所以我不能在这里使用Input()或[]绑定。

Also, because multiple instances could cause issues I won't get into here, we're not using a service to get and share data between components. 此外,由于多个实例可能会导致我不会进入此处的问题,因此我们不会使用服务来获取和共享组件之间的数据。 So I can't inject a service to get the same instance in the dialog component (EnrollingProcessComponent) either. 所以我不能注入一个服务来获取对话框组件(EnrollingProcessComponent)中的相同实例。

So, all that said, I need to somehow pass this data (which is simply an email address) from the originating component to the dialog component. 所以,所有这些,我需要以某种方式将这些数据(这只是一个电子邮件地址)从原始组件传递到对话框组件。 I assume I should be able to do this by passing it as a parameter, but so far I'm not able to get it to work (ie, when I console out the value in the originating component, I get the value. But when consoling that value out in the dialog (EnrollingProcessComponent) component, I get 'undefined'). 我假设我应该能够通过将其作为参数传递来实现这一点,但到目前为止我无法使其工作(即,当我控制原始组件中的值时,我得到了值。但是当在对话框(EnrollingProcessComponent)组件中安慰该值,我得到'undefined')。

I use a click() event to open the dialog component: 我使用click()事件打开对话框组件:

<button *ngIf="canBeEnrolled()" md-menu-item 
(click)="onContactClicked()">
  <span class="md-menu-text">Initiate Contact</span>
</button>

And the function that's triggered looks like this: 并且触发的功能如下所示:

public onContactClicked(primaryContactEmail): void {
    primaryContactEmail = this.getPrimaryContactEmail();
    console.log(this.getPrimaryContactEmail());
    console.log('onContactClicked engaged on: ' + new Date());
    // Create dialog
    let dialogRef: MdDialogRef<EnrollingProcessComponent>
        = this.dialog.open(EnrollingProcessComponent, {disableClose: true});
}

How can I pass the result of the getPrimaryContactEmail(), which is an email address, from the originating component to the component fired when the dialog opens? 如何将getPrimaryContactEmail()的结果从原始组件传递到对话框打开时触发的组件,这是一个电子邮件地址?

You can pass values to the component instance via the data property of MdDialogConfig options like this: 您可以通过MdDialogConfig选项的data属性将值传递给组件实例,如下所示:

primaryContactEmail = this.getPrimaryContactEmail();

let dialogRef: MdDialogRef<EnrollingProcessComponent>
    = this.dialog.open(EnrollingProcessComponent, {
            disableClose: true,
            data: { primaryContactEmail: primaryContactEmail }
        });

You would then need to inject MD_DIALOG_DATA into the component EnrollingProcessComponent component, which would allow you to access any passed data, in this case a property named primaryContactEmail : 然后,您将需要注入MD_DIALOG_DATA到组件EnrollingProcessComponent组件,这将允许您访问任何传送的数据,在这种情况下,命名属性primaryContactEmail

import {MdDialogRef, MD_DIALOG_DATA} from '@angular/material';

@Component({
  selector: 'example-dialog',
  templateUrl: 'example-dialog.html',
})
export class DialogResultExampleDialog {
  constructor(
      public dialogRef: MdDialogRef<DialogResultExampleDialog>, 
      @Inject(MD_DIALOG_DATA) public data: any) {
      console.log(this.data);
      console.log(this.data.primaryContactEmail);
  }
}

Here is a plunker demonstrating the functionality. 这是一个展示功能的plunker Check the console when you open the dialog to see the data being loggable. 打开对话框时检查控制台以查看可记录的数据。

If you need to pass the value back to the parent component you could use md-dialog-close or close() to pass the value back up. 如果需要将值传递回父组件,可以使用md-dialog-closeclose()来传递值。

I've added closing the dialog from within the component via close(value: any) and passing a value to the parent calling component. 我添加了通过close(value: any)从组件中关闭对话框并将值传递给父调用组件。 Ignore the initial errors on load, those were present on the base unaltered example. 忽略加载时的初始错误,这些错误存在于未更改的基本示例上。

Hopefully that helps! 希望这有帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM