gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:10:16 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com