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

时间:2018-12-13 11:12:29

标签: 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)
    );

  }

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

3 个答案:

答案 0 :(得分:1)

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

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

它应该看起来像这样。

答案 1 :(得分:1)

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

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

transaction-pool.component.ts

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

答案 2 :(得分:0)

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

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