Topic: The solution given in the url https://mdbootstrap.com/support/angular/calling-modal-in-component-from-another-component/ does not work from a service
                  
                  birmain
                  pro
                  asked 7 years ago
                
                 
Hello 
The solution given in the url  works perfectly when the service is invoked from a component, which resolves the issue I sent.
But when invoked from a CanDeactivate Guard Service no error occurs, but the modal window is not displayed when running showModal () This is the code of my service
                
                
                
                  
                  
                  
                    
                    
                
              
              
              
            import { CanDeactivate } from '@angular/router';
import { ModalService } from './simple.modal.service';
import { Injectable } from '@angular/core';
export interface FormComponent {
hayCambios:boolean;
}
@Injectable()
export class UnsavedChangesGuardService implements CanDeactivate<FormComponent> {
  constructor(privatemodalDialogService:ModalService) {}
canDeactivate(component:FormComponent) {
if (component.hayCambios) {
this.modalDialogService.showModal();
}
return true;
}
}
 
                
                  
                      
                        Add comment
                      
                    
                  
                
                      
                      Arkadiusz Idzikowski
                      staff
                        answered 7 years ago
                    
                    Dear birmain,
I think it's a problem with canDeactivate method implementation. There is no condition that would stop guard from changing the view. Please check if the code inside the if statement is executed at all and try to add 'return false' after 'this.modalDialogService.showModal()'. If that won't help, please send an example app to a.idzikowski@mdbootstrap.com so I can debug this problem properly.
Regards,
Arek
                    
                      
                    
                    
                    
                    
                  
                  
                FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Status
              Answered
Specification of the issue
              - ForumUser: Pro
- Premium support: No
- Technology: MDB Angular
- MDB Version: 6.2.2
- Device: PC
- Browser: Chrome
- OS: Windows 10
- Provided sample code: Yes
- Provided link: Yes