Angular:将数据从材料对话框传递到未打开对话框的组件 [英] Angular: Pass data from Material Dialog to component, which didn't open the dialog

查看:19
本文介绍了Angular:将数据从材料对话框传递到未打开对话框的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 network.component 可以打开我的对话框 send-tx-dialog.component.用户在 send-tx-dialog 中填写信息.我想将该信息发送到另一个组件:transaction-pool.component.之后我想动态显示数据表中的数据.我尝试使用 push() 但它不起作用.

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 component: transaction-pool.component. And after that I want to display the data in a data table dynamically. I tried to use push() but it didn't work.

有什么方法可以做到这一点?

What is a possible way to do that?

遵循一些我认为可能很重要的代码.

Following some code which I think could be important.

对话的部分TS:

  constructor(
    private fb: FormBuilder,
    private dialogRef: MatDialogRef<SendTXDialogComponent>,
    @Inject(MAT_DIALOG_DATA) data) {

    this.sender = data.sender;

    this.form = this.fb.group({
      sender: [this.sender, Validators.required],
      recipient: [this.recipient, Validators.required],
      amount: [this.amount, Validators.required],
      fee: [this.fee, Validators.required],
      releasedAt: [moment(), Validators.required]
    });
  }

对话框在 network.component.ts 中打开:

Dialog gets opened in network.component.ts:

  openDialog(nodeID) {

    const dialogConfig = new MatDialogConfig();

    dialogConfig.disableClose = true;
    dialogConfig.autoFocus = true;
    dialogConfig.hasBackdrop = false;

    dialogConfig.data = {
      sender: nodeID,
    };

    const dialogRef = this.dialog.open(SendTXDialogComponent, dialogConfig);

    dialogRef.afterClosed().subscribe(
      data => console.log('Send Transaction Output:', data)
    );

  }

事务池.component.ts:

transaction-pool.component.ts:

export class TransactionPoolComponent implements OnInit {
  displayedColumns = ['sender', 'recipient', 'amount', 'fee'];
  dataSource = ELEMENT_DATA;

  transaction: Transaction;

  constructor(private _AS: AuthenticationService) {
  }

  ngOnInit() {
    this._AS.transaction.subscribe( transaction => {
      this.transaction = transaction;
      this.dataSource.push(this.transaction); // This doesn't display the data in the table
      }
    );
  }
}

export interface Transaction {
  sender: number;
  recipient: string;
  amount: number;
  fee: string;
}

const ELEMENT_DATA: Transaction[] = [
];

推荐答案

我不知道这是解决问题的正确方法,但如果它对你有用,我会尽力提供帮助

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.html

<button mat-raised-button (click)="openDialog()">Click</button>

network.component.ts

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');
        });
      }

对话 TS:

@Component({
  selector: 'app-dialog',
  templateUrl: 'dialog.html',
})
export class DialogOverviewExampleDialog {
  form: FormGroup;
  constructor(
    public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
    @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

dialog.html

<div [formGroup]="form">
  <mat-form-field class="example-full-width">
    <input matInput placeholder="first" formControlName="sender">
  </mat-form-field>
  <mat-form-field class="example-full-width">
    <input matInput placeholder="second" formControlName="recipient">
  </mat-form-field>
<mat-form-field class="example-full-width">
    <input matInput placeholder="second" formControlName="amount">
  </mat-form-field>
<mat-form-field class="example-full-width">
    <input matInput placeholder="second" formControlName="fee">
  </mat-form-field>
<mat-form-field class="example-full-width">
    <input matInput placeholder="second" formControlName="releasedAt">
  </mat-form-field>
 <button [mat-dialog-close]="form.value">save</button>
</div>

授权服务:

 export class AuthService {

     public transaction = new BehaviorSubject<any>(false);
  transaction$ = this.transaction.asObservable();

  emitTransaction(data: any) {
    this.transaction.next(data);
  }

事务池.component.ts

transaction-pool.component.ts

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

这篇关于Angular:将数据从材料对话框传递到未打开对话框的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆