After reviewing your code, it appears that your service will need information about the view in order to function properly.
One solution is to include an instance of the modal within your service whenever the modal is opened. This way, you can easily reference that instance and close it using the service.
Here is a hypothetical example to illustrate the concept:
// Sample component
export class MyComponent implements OnInit {
modal: Modal;
constructor(private modalService: ModalService) {}
ngOnInit() {
this.modalService.modal = this.modal;
}
close() {
this.modalService.close();
}
}
// Sample service
export class ModalService {
_modal: Modal;
set modal(modal: Modal) {
this._modal = modal;
}
close() {
if (this._modal) {
this._modal.close();
this._modal = null;
}
}
}
By utilizing this method, you create flexibility as any component can close the modal simply by injecting the ModalService
.