Angular: Pass data from Material Dialog to component, which didn't open the dialog

前端 未结 3 857
陌清茗
陌清茗 2020-12-12 02:56

I have a network.component which opens my dialog send-tx-dialog.component. The user fills the send-tx-dialog with information. I want to send that information to another com

3条回答
  •  半阙折子戏
    2020-12-12 03:41

    I don't know this is the right way to solve problem but i try to help if it's work for you

    network.component.html

    
    

    network.component.ts

     constructor(private fb: FormBuilder, private _AS: AuthService, public dialog: MatDialog) {}
        openDialog(): void {
            const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
              width: '250px',
              data: ''
            });
    
            dialogRef.afterClosed().subscribe(result => {
              this._AS.emitTransaction(result);
              console.log('The dialog was closed');
            });
          }
    

    dialog TS:

    @Component({
      selector: 'app-dialog',
      templateUrl: 'dialog.html',
    })
    export class DialogOverviewExampleDialog {
      form: FormGroup;
      constructor(
        public dialogRef: MatDialogRef,
        @Inject(MAT_DIALOG_DATA) public data: any, private fb: FormBuilder) {
       this.form = this.fb.group({
          sender: ['', Validators.required],
          recipient: ['', Validators.required],
          amount: ['', Validators.required],
          fee: ['', Validators.required],
          releasedAt: [moment(), Validators.required]
        });
      }
      onNoClick(): void {
        this.dialogRef.close();
      }
    }
    

    dialog.html

    AuthService:

     export class AuthService {
    
         public transaction = new BehaviorSubject(false);
      transaction$ = this.transaction.asObservable();
    
      emitTransaction(data: any) {
        this.transaction.next(data);
      }
    

    transaction-pool.component.ts

    ngOnInit() {
        this._AS.transaction$.subscribe(data => {
          this.data = data;
          console.log(this.data);
        });
      }
    

提交回复
热议问题