繁体   English   中英

角度:将数据从“材质对话框”传递到组件,但未打开对话框

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

我有一个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)
    );

  }

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

在共享服务中,您可以执行以下操作:

public transaction = new Subject<any>();

emitTransaction(val) {
  this.transaction.next(val);
}

在关闭对话框的子区域之后执行以下操作:

dialogRef.afterClosed().subscribe(
      data => this.sharedService.emitTransaction(data);
    );

在您的其他组件中:

this.sharedService.transaction.subscribe(transaction => {
    this.transaction = transaction;
  })

它看起来应该像这样。

我不知道这是解决问题的正确方法,但我会尽力帮助您

network.component.html

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

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

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

AuthService:

 export class AuthService {

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

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

交易pool.component.ts

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

您应该使用共享服务。 在ngAfterViewInit中,将对话框组件订阅到要在共享服务中侦听的属性。 在父组件中调用共享服务方法,该方法将在共享服务中设置属性。 属性应为Subject(rxjs)类型

看一下这篇文章:您将了解所有内容。 具有共享服务的部分。 https://netbasal.com/event-emitters-in-angular-13e84ee8d28c只要确保您在对话框组件中订阅了ngAfterViewInit。

暂无
暂无

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

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