I have the following app structure:
- app
- nav
In the NavComponent, I am trying to show an element in the AppComponent. All I want to do is call the showElement() function inside the AppComponent from the NavComponent. How in the world do I do this?
I've looked through so many examples of services, events, and so forth, and I've tried many of them and could never get my head wrapped around the terminology and/or syntax to get them to work. If someone wouldn't mind giving me a nudge here, I would appreciate it. It can't be this hard to do.
Edit with More Details Below
Thanks, @Eliseo. These are helping, somewhat, but I'm still struggling to get it to fit what I am trying to do. Let me try to flesh out the structure more and perhaps an answer for my specific attempt would help. I will mention I've read the details on Observables about five times so far, and it only sinks in about 50% for whatever reason. I think it's because I don't understand how it functions behind-the-scenes.
Regardless, here is what I want to do:
App Component
There will be multiple page components using a similar structure but changed out with navigation. Each page component will manipulate the bridgeService data in order to show the modal. this.bridgeService.showModal and this.bridgeService.url needs to be updated and the app needs to act on it when the service is updated via a page component (or other component). [I know the syntax below is not correct, mainly because I don't understand the bigger picture.]
html
<page></page>
<app-modal *ngIf="{{this.bridgeService.showModal}}" [url]="{{this.bridgeService.url}}"></app-modal>
ts
export class AppComponent {
constructor(public bridgeService: BridgeService) {}
}
Bridge Service
Use a service to share these values (a boolean and a URL) between components; these need to be updated by various components and components need to update themselves when these values change. The boolean will turn on/off the Modal component. The URL will affect what is displayed in the Modal component.
ts
public showModal = false;
public modalURL = '';
// not sure how to make these communicate between and update
// the components as observables, eventEmitters, etc.
Page Component
There will be about one hundred of these. Navigation will cycle through these, updating the app component with the specific page component to display. There will be various stages in each page in which a modal should be displayed. These will be custom for each page. Some pages may not show any. Others may need to show more than one, depending on the stage. There is a bit more involved, but if I can get a working example of how to share this data and update the components based on a change in this data, then I can take it from there.
ts
if (this.stage == 3) {
// show modal
this.bridgeService.url = "http://custom.com";
this.bridgeService.showModal = true;
} else {
// turn off modal for other stages
this.bridgeService.showModal = false;
}