admin管理员组文章数量:1399995
I'm having trouble managing data communication between a parent component and a modal child component in an Angular application. The parent component opens a modal (child component), and after an event (such as scanning a barcode), I need to send the data (the barcode) back to the parent component.
I'm using an EventEmitter
to emit this data from the child component to the parent.
However, when I try to emit the data, the parent doesn't receive the expected data. Instead, I get undefined
.
- The parent component calls
this.dialogService.showModal()
to display the modal. - The child modal component processes the barcode scan and emits the data using
EventEmitter
. - The parent component listens for this emitted event, but the result is always undefined.
Function on parent component:
public async startScan(): Promise<void> {
const modal = await this.dialogService.showModal({
component: ScannerComponent,
cssClass: 'barcode-scanning-modal',
showBackdrop: false,
componentProps: {
pushData: new EventEmitter<Barcode>()
},
});
}
child component:
pushData = output<Barcode>();
private processDetectedBarcode(barcode: Barcode): void {
this.playBeepSound();
this.pushData.emit(barcode);
}
I'm having trouble managing data communication between a parent component and a modal child component in an Angular application. The parent component opens a modal (child component), and after an event (such as scanning a barcode), I need to send the data (the barcode) back to the parent component.
I'm using an EventEmitter
to emit this data from the child component to the parent.
However, when I try to emit the data, the parent doesn't receive the expected data. Instead, I get undefined
.
- The parent component calls
this.dialogService.showModal()
to display the modal. - The child modal component processes the barcode scan and emits the data using
EventEmitter
. - The parent component listens for this emitted event, but the result is always undefined.
Function on parent component:
public async startScan(): Promise<void> {
const modal = await this.dialogService.showModal({
component: ScannerComponent,
cssClass: 'barcode-scanning-modal',
showBackdrop: false,
componentProps: {
pushData: new EventEmitter<Barcode>()
},
});
}
child component:
pushData = output<Barcode>();
private processDetectedBarcode(barcode: Barcode): void {
this.playBeepSound();
this.pushData.emit(barcode);
}
Share
Improve this question
edited 2 days ago
Chait
1,3612 gold badges22 silver badges37 bronze badges
asked Mar 26 at 16:49
Hardy RakotoHardy Rakoto
112 bronze badges
1
- what ui library are you using please share more code, like imports and the dialog TS code – Naren Murali Commented Mar 26 at 16:54
1 Answer
Reset to default 0Pass an eventEmitter as an property of componentProps
inside the object(2nd parameter) of open
method.
emitter: EventEmitter<Barcode> = new EventEmitter<Barcode>();
public async startScan(): Promise<void> {
const modal = await this.dialogService.showModal({
component: ScannerComponent,
cssClass: 'barcode-scanning-modal',
showBackdrop: false,
componentProps: {
pushData: this.emitter
},
});
}
Then we can read this on the child using @Input
or input
.
pushData = input<EventEmitter<Barcode>>();
private processDetectedBarcode(barcode: Barcode): void {
this.playBeepSound();
this.pushData().emit(barcode);
}
本文标签:
版权声明:本文标题:Issue with data communication between a modal child component and a parent component using Output in Angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744137476a2592465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论