I've build a service that shares data between components following the answer provided by SrAxi . All works fine until in the receiver component I implement the subscribe to the subject: the response and nothing else inside that subscribe method is showing, neither a simple console.log of a string; it is like Angular doesn't enter in that method at all.
I tried to do the subscription into OnInit, AfterViewInit and AfterContentInit: nothing changed. Also used setTimeOut(); nothing.
Then I've found the answer of Julius Dzidzevičius and then changed the subject into behaviorSuject and subscribed to it; again nothing changed.
This is driving me crazy, hope you could help
Code: Sender component
private actionsInfoObj = {
fromAction: null,
showForm: null,
data: null
};
.....
addNewRow() {
this.actionsInfoObj.fromAction = 'addRow';
this.actionsInfoObj.showForm = true;
this.bus.actionInfo(this.actionsInfoObj);
this.addRow.emit( this.actionsInfoObj );
}
Service (ButtonsUtilitiesService)
getActionInfo$: Observable<any>;
private getActionInfoSubject = new Subject<any>();
constructor() {
this.getActionInfo$ =
this.getActionInfoSubject.asObservable();
}
actionInfo(data) {
console.log(data);
this.getActionInfoSubject.next(data);
}
Receiver component
constructor(
private fb: FormBuilder,
private bus: ButtonsUtilitiesService
) {
this.bus.getActionInfo$.subscribe( (data: object) => {
this.actionInfo = data;
})
}
EDIT: forgot to say that (for now) both sender and receiver are in the same parent component:
<section *ngIf="!showForm ">
<div>
<app-table (addRow)="addRow($event)"></app-table>
</div>
</section>
<section *ngIf="showForm" class="mat-elevation-z8">
<div>
<app-dynamic-form></app-dynamic-form>
</div>
</section>