Prompt user to save changes or cancel before closing modal (if closed by pressing ESC or clicking the backdrop)

When I manually close the modal, everything works fine. I just create a prompt and only call the BsModalRef.hide() method when the prompt (sweetalert) is closed.

However, when the modal is closed using the ESC key or click-outside events provided by Bootstrap, I am still able to subscribe to the BsModalService.onHide event. But I'm unsure how to prevent the modal from closing until the prompt has been answered. bsModalRef: BsModalRef;

constructor(injector: Injector,
            public bsModalService: BsModalService) {
    super(injector);
}

ngOnInit(): void {
    this.bsModalRef = this.bsModalService.show(SomeFormComponent,
        {
            keyboard: true,
            backdrop: true
        });

    this.bsModalService.onHide.subscribe((e) => {
        // ISSUE: modal already closed at this point...
        swal({
            text: 'Do you want to save changes?',
            title: 'Prompt',
            icon: 'warning'
        }).open().then((answer: ModalResult) => {
            switch(answer) {
                case ModalResult.Save:
                    // I want to save and then close the modal
                    break;
                case ModalResult.DontSave:
                    // Close the modal
                    break;
                default:
                    // Stay on the modal
                    break;
            }
        });
    });
}

Any help would be greatly appreciated. Thank you!

Answer №1

I think you're referring to Bootstrap when you mention Bs, and it includes a beforeDismiss Event. You should give that a try.

Answer №2

Looks like ngx-bootstrap doesn't offer that specific Event.

To resolve this problem, I switched to using @ng-bootstrap instead of the ngx-bootstrap package. Additionally, I needed to update my stylesheets from bootstrap 3 to bootstrap 4.

Now I am able to utilize the beforeDismiss-Event without any issues.

Appreciate the help!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The error persists in Ajax requests despite the correct usage of json_encode and everything functioning correctly

In the realm of e-commerce, I have successfully implemented an AJAX request to dynamically add and remove items from the cart. Interestingly enough, every time the jQuery script is executed, everything seems to be working seamlessly. The server always resp ...

Is it possible to make changes to local storage data without impacting the rest of the data set?

https://i.sstatic.net/BBcJF.pngI am looking for a way to modify specific data in the local storage without affecting any other stored information. However, I have encountered an issue where editing values works correctly for the first three attempts, but ...

What is the best way to retrieve environment variables from an NPM package in an Angular 5 application

Is there a method for my node module, created from an Angular 5 application, to access the environment variable from the Angular 5 application (environments/environment.ts)? Perhaps Angular 5 exports its environment variables to JavaScript global variables ...

Set up local npm packages for easy access by different projects

Can someone explain to me how npm works compared to Maven (I have a background in Java) when it comes to package management? I've developed a generic component using Angular 4 that will be used across multiple projects. I've published it to our n ...

Dropdown element with PrimeNG adorned with a span

I am trying to create a simple form with text inputs and dropdowns. I have successfully implemented the textInput, but I am struggling with the dropdowns. Below is the code that I have so far: <div class="p-grid p-dir-col p-offset-2"> ...

Partial view encountering Dropzone JS function error

I am currently using jquery-modal by Kyle Fox and I've run into a problem. Whenever I try to open the modal window, my partial view gets loaded into it. However, I keep seeing this error in the console: Dropzone.options.dropzone is not recognized a ...

Tips for uploading information to a web service

Issue Description:- I am attempting to access web services from a different domain (i.e. Systems are not locally connected) and encountering the following error: "Failed to load : Response for preflight is invalid (redirect)" var username = "<a href= ...

What is the process of transforming the content of a file into an array using JavaScript?

I have a file named "temperatures.txt" that contains the following text: 22, 21, 23 Is there a way to transfer the contents of this file into a JavaScript array? const weatherData = [22, 21, 23]; ...

Split total based on checkboxes toggled with JavaScript

I'm facing a challenge. I have a total sum of donations collected, for example, 10€. Additionally, there are various NGOs that one can select to donate to. Individuals have the option to toggle on/off which NGO's they wish to contribute toward ...

Leveraging the package.json file to execute a separate script within the package.json file

Within my project's package.json file, I have a script called npm run script1. Additionally, my project includes a private npm package as a dependency, which contains its own set of scripts in its package.json file, including one named script2. My goa ...

Troubleshooting Angular HTTP: Issue with the HTTP request headers not updating

// assigning the httpclient protected _http: HttpClient = inject(HttpClient); // defining the options for the request const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/tcc' }), observe: 'resp ...

Angular repeatedly triggering cloud function

Currently facing an issue in my angular project where a cloud function is being triggered multiple times. The console.log('call') statement is appearing three times. this.profileList$ = this.usersService.profileList(this.route.snapshot.paramMap.g ...

The "results per page" ajax dropdown in Yii stops working after other ajax content is loaded on the page

One of the views in Yii framework is the "Patients" view, which contains several CGridViews and other elements that are loaded via ajax after the initial view has loaded. Some of these elements are nested within multiple levels of ajax-loaded divs. Within ...

How to efficiently filter an array containing nested objects using TypeScript

I'm currently working with a list of menus and submenus: [ { "NUA_ID_Menu": 1, "TXT_Nom_Menu": "Menu 1", "Liste_Sous_Menus": [ { "TXT_Nom_Menu": ...

Transfer elements from collections employing multer

Data Payload Example:- education[0][university]: jru education[0][degree]: mca education[0][specialization]: computer application education[0][certificate_pdf_url]: (binary) education[0][degree_type]: Teaching Degree education[0][years_of_study]: [&q ...

react-datepicker displaying error message "Preventing default action not possible within a passive event listener invocation"

I've integrated the react-datepicker library in portal mode. While it functions well on browsers, I encounter an error when using mobile browser mode. An issue arises stating: "Unable to preventDefault inside passive event listener invocation" Des ...

CSS3 Perspective Issue: Solutions for Fixing

I'm currently working on creating a card flip animation that flips in the X axis direction. Right now, the div rotates using the rotateX() method. I attempted to add the perspective property to the upper div, but it ended up distorting my div structur ...

Is there a way to bring in data from a .d.ts file into a .js file that shares its name?

I am in the process of writing JavaScript code and I want to ensure type safety using TypeScript with JSDoc. Since it's more convenient to define types in TypeScript, my intention was to place the type definitions in a .d.ts file alongside my .js fil ...

Incorporating Interpolation into Angular 2 Routing

I have a dropdown menu for selecting categories, and based on the selected category, I want to update the router link for the menus. For example, if I select Category1, then the router link for SubMenu1 should be BASE/category1/submenu1. Similarly, for Cat ...

The 'data' variable is not defined in the React custom hook Pagination

I am currently working with an API that shows music categories on a browser, and I'm attempting to create a custom pagination hook. However, I keep encountering an error stating "object is not iterable." I am new to custom hooks and would appreciate a ...