Angular:将数据从材质对话框传递到组件,组件没有打开对话框 [英] 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 部分:
构造函数(私人 fb:FormBuilder,private dialogRef: MatDialogRef,@Inject(MAT_DIALOG_DATA) 数据) {this.sender = data.sender;this.form = this.fb.group({发件人:[this.sender, Validators.required],收件人:[this.recipient, Validators.required],金额:[this.amount, Validators.required],费用:[this.fee, Validators.required],发布时间:[moment(), Validators.required]});}
对话框在 network.component.ts 中打开:
openDialog(nodeID) {const dialogConfig = new MatDialogConfig();dialogConfig.disableClose = true;dialogConfig.autoFocus = true;dialogConfig.hasBackdrop = false;dialogConfig.data = {发件人:节点ID,};const dialogRef = this.dialog.open(SendTXDialogComponent, dialogConfig);dialogRef.afterClosed().subscribe(数据 =>console.log('发送交易输出:', 数据));}
transaction-pool.component.ts:
export class TransactionPoolComponent 实现 OnInit {displayColumns = ['sender', 'recipient', 'amount', 'fee'];数据源 = ELEMENT_DATA;交易:交易;构造函数(私有_AS:AuthenticationService){}ngOnInit() {this._AS.transaction.subscribe( 交易 => {this.transaction = 交易;this.dataSource.push(this.transaction);//这不显示表中的数据});}}出口接口交易{发件人:号码;收件人:字符串;数量:数量;费用:字符串;}const ELEMENT_DATA: 交易[] = [];
我不知道这是解决问题的正确方法,但如果对您有用,我会尽力提供帮助
network.component.html
network.component.ts
构造函数(私有 fb:FormBuilder,私有 _AS:AuthService,公共对话框:MatDialog){}openDialog(): void {const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {宽度:'250px',数据: ''});dialogRef.afterClosed().subscribe(result => {this._AS.emitTransaction(result);console.log('对话框已关闭');});}
对话 TS:
@Component({选择器:'应用程序对话框',templateUrl: 'dialog.html',})导出类 DialogOverviewExampleDialog {表格:表格组;构造函数(public dialogRef: MatDialogRef,@Inject(MAT_DIALOG_DATA) 公共数据:任意,私有 fb:FormBuilder) {this.form = this.fb.group({发件人:['',Validators.required],收件人:['', Validators.required],金额:['',Validators.required],费用:['',Validators.required],发布时间:[moment(), Validators.required]});}onNoClick(): void {this.dialogRef.close();}}
对话框.html
<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">保存</button>
身份验证服务:
导出类 AuthService {公共事务 = 新的 BehaviorSubject(false);交易$ = this.transaction.asObservable();发射交易(数据:任何){this.transaction.next(数据);}
transaction-pool.component.ts
ngOnInit() {this._AS.transaction$.subscribe(data => {this.data = 数据;控制台日志(this.data);});}
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.
Part of the TS of the dialog:
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]
});
}
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)
);
}
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
<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');
});
}
dialog 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);
});
}
这篇关于Angular:将数据从材质对话框传递到组件,组件没有打开对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!