I try to pass a form from my child component to his parent.
Child TS:
@Output() submit: EventEmitter<FormGroup> = new EventEmitter();
updateStream(): void {
const body = {some data};
// If I put this.submit.emit(this.form) here it works
this.apiGatewayService.verifyKeys(body).subscribe(
(res) => {
console.log('SUCCESS verify : ', res);
this.submit.emit(this.form); // doesn't work
},
(error) => {
console.log('ERROR verify : ', error);
this.disableLoader.emit(); // doesn't work
});
}
The html :
<child (submit)="updateStream($event)"></child>
Parent TS :
updateStream(form: formGroup): void {
console.log('UPDATE');
}
All my emits into subscribe doesn't work (console.log in parent composant not displayed). However outside the subscribe it works.
Have you an idea ?
EDIT
the problem is with the line this.apiGatewayService.verifyKeys(body). This call works perfectly (correct response), however the emit doesn't work. However, it works, if I mock this call like this :
updateStreamOut(): void {
this.test().subscribe(
(res) => {
console.log('SUCCESS verify : ', res);
this.submit.emit(this.lambdaForm); // Works
},
(error) => {
console.log('ERROR verify : ', error);
this.verifyError = true;
this.disableLoader.emit(); // Works
}
);
}
test(): Observable<any> {
return Observable.of({ status: 'ok' });
}
Here my function verifyKeys :
verifyKeys(body): Observable<any> {
const result = Observable.fromPromise(
this.client.externalVerifyPost({}, body, config.additionalParams)
);
return this.handleResult.dispatchResponse(result, false);
}
HandleResult service :
dispatchResponse(result: Observable<any>, enableSnackbar: boolean = true) {
return result
.catch((error) => {
const exception = this.getException(error);
this.apiResponse.errorsHandler(exception);
return Observable.throw(exception);
})
.mergeMap((res) => {
if (enableSnackbar) {
this.apiResponse.successHandler(res);
}
return Observable.of(res.data);
});
}
Maybe it gives you more informations.
I found my error. Indeed, when this.apiGatewayService.verifyKeys(body) was launch a loader was displayed.
My parent HTML was build like this :
<div *ngIf="!loader">
<child ...></child
</div>
<div *ngIf="loader>
My loader
</div>
So when my loader was display, my component was delete from the DOM and my "submit" event emitter was unsubscribe.
To prevent that, I use the angular property [hidden] instead of *ngIf. [Hidden] act like a display: none, and not delete my child component from the DOM.