Angular:将数据从 Material 对话框传递到未打开对话框的组件

标签 angular typescript dialog components communication

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

有什么可能的方法来做到这一点?

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

对话框的部分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中打开:

  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:

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[] = [
];

最佳答案

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

网络.组件.html

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

网络.组件.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

<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

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

关于Angular:将数据从 Material 对话框传递到未打开对话框的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53760569/

相关文章:

javascript - 在 JavaScript 中单击类名称后更改类名称

angular - 如果连接不处于 'Connected' 状态,则无法发送数据

mongodb - 使用 mongoose 并利用我已经定义的 schmas 在 NestJS 中做种子 mongoDB 的正确方法是什么

javascript - TS 如何在方法内使用类变量?

java - 使用预设选项捕获 JOptionPane 输入对话框的值?

node.js - 在 Node 与 nginx 中服务 Angular

Angular 2 多槽嵌入

javascript - TypeScript:在运行时区分未设置和不存在的属性

c++ - 如何在 MFC Dialog 应用程序中插入更多对话框?

java - 区分OnDismiss和dialogfragment的方法 - Android Studio