Angular: 将数据从 Material Dialog 传递到未打开对话框的组件
Angular: Pass data from Material Dialog to component, which didn't open the dialog
我有一个 network.component 可以打开我的对话框 send-tx-dialog.component。用户用信息填充发送发送对话框。我想将该信息发送到另一个组件:transaction-pool.component。
之后我想动态显示数据 table 中的数据。我尝试使用 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>
授权服务:
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)
类型
看看这个post:你就会明白一切。
共享服务部分。
https://netbasal.com/event-emitters-in-angular-13e84ee8d28c
只需确保您在对话框组件的 ngAfterViewInit 中订阅即可。
我有一个 network.component 可以打开我的对话框 send-tx-dialog.component。用户用信息填充发送发送对话框。我想将该信息发送到另一个组件:transaction-pool.component。 之后我想动态显示数据 table 中的数据。我尝试使用 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>
授权服务:
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)
类型看看这个post:你就会明白一切。 共享服务部分。 https://netbasal.com/event-emitters-in-angular-13e84ee8d28c 只需确保您在对话框组件的 ngAfterViewInit 中订阅即可。