I am working on a large Angular 2 app separated in different modules, like so:
- app.module:
- user.module
- supplier.module
- shared.module
- etc.
From my shared module, I am calling a Bootstrap 4 modal component for all my modals in the app, that I customize through a modal configuration object using the @Input decorator. Here's what my modal component .ts looks like:
modal.component.ts:
export class ModalComponent {
constructor() { }
public modalMethods = new ModalActionMethods();
@Input() modalConf: ModalModel;
}
Then from inside the modal template, I am referencing the modalConf object, like so:
modal.template.html:
<div class="bg-modal-container" [hidden]="!modalConf.isOpen">
Finally, I pass the data to the modal component when I use it in the view of whatever component needs it:
whatever.component.html:
<modal [modalConf]="modalOptions"></modal>
modalOptions is referencing an object defined inside my whatever.component's main class.
whatever.component.ts
export class WhatevertComponent {
private modalOptions = {
isOpen: false,
title: "Some Modal Title"
// etc.
}
}
So far so good.
Now, for my users module, since I know I will be using my modal component a lot across multiple components, I would like to do things a little differently, since this module serves different components depending on the route:
users.routing.ts:
const routes: Routes = [
{
path: '', component: UsersComponent,
children: [
{ path: 'creation', component: UserCreationComponent },
{ path: 'management', component: UserManagementComponent }
]
}
];
The question:
What I would like to do is insert my modal component inside my main UsersComponent (placing the modalOptions object inside it's main class and the modal selector in the view, same as above for whatever-component), but also be able to somehow modify the modal configuration from within other child components, such as creation, management, etc., so that I can show relevant child component data in my modal. What I'm trying to avoid is to insert (and create a separate modalConf object for) my modal component inside every child component that will be using/manipulating it.
What would be the best approach to do this? I am open to suggestions.