Is there a way to capture the backdrop click event when clicking outside of an Angular UI Bootstrap modal?

I have encountered an issue in my application where I am using the $modal.open() function to display a modal popup that uses another page as a template. The popup works fine when clicked, and the Cancel button also functions correctly triggering the specified function.

    $scope.cancel=function(){
    });

However, I am facing a problem when trying to capture the event of clicking outside the modal popup using the following code:

    $scope.dismiss=function(){
    });

Is there a way to catch this event?

I have researched many AngularJS resources but have not been able to find a solution to this issue.

Answer №1

$modal.open() function returns an object containing a promise. This promise can be utilized by chaining it and handling it within the catch block. When the user clicks on the background outside the modal, it triggers a dismiss action internally which in turn rejects the promise.

For example:

var instance = $modal.open(...);

instance.result.then(function(){
  // Executes when the modal is closed
}, function(){
  // Executes when the modal is dismissed
});

If you are implementing this functionality in a child component where $modalInstance is injected, you can also handle it there. Essentially, instead of managing events directly, promises provide a cleaner way to handle modals at a higher level.

Answer №2

If you want to prevent the modal from being dismissed when clicked outside, you can utilize the following code snippet:

backdrop: 'static'

Simply add this line of code in your modal options like shown below:

var modalInstance = $modal.open({
    templateUrl: 'myModalContent.html',
    controller: 'ModalInstanceCtrl',
    backdrop: 'static',
    ...
});

To understand more about how backdrop: 'static' works, check out the Bootstrap 3.0 Documentation. This specifies a static backdrop that won't close the modal when clicked.

Answer №3

Detect any clicks on the entire html page and automatically close the popup window.

If there are any clicks occurring within the popup window, prevent them from triggering any additional events.

For example:

$("html").on("click", closeModal());

$(".popup-window").on("click", function(event){
   event.stopPropagation();
}

Answer №4

I encountered a similar issue with the Angular Mobile UI Demo (1.2). Interestingly, the modal was not defined in the main JS file within the demo files.

To resolve this issue, I simply added two additional variables to the code snippet in modal1.html.

<div class="modal-dialog" ui-outer-click="Ui.turnOff('modal1')" ui-outer-click-if="Ui.active('modal1')">

For further clarification, you can visit:

Both of these properties are essential for proper functionality.

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

When a Vue.js datepicker is set as required, it can be submitted even if

When using the vuejs-datepicker, setting the html required attribute on input fields may not work as expected. This can lead to the form being submitted without an input value. <form> <datepicker placeholder="Select Date" required></datep ...

Icons are failing to display in the dropdown menu of MUI after an option is selected in ReactJS

There seems to be an issue with the icon in the select tag. Initially, it is not showing which is correct. However, when you click the dropdown, the icon appears as expected. But after selecting an option, if you click the dropdown again, the icon disapp ...

Failure to execute Ajax request when searching for a query

My personal GitHub profile viewer React application allows you to search for a profile by username. By default, my own GitHub username is provided on the landing page. However, when trying to search for another user, my ajax call is not functioning properl ...

"Encountering an error message stating "Cannot access SpreadsheetApp.getUi() within this context" when attempting to execute the

I've encountered an issue while trying to access the UI object in Apps Script. The code I'm using is something I've used before without any problems, but now I'm getting an error message that says "Cannot Call the .getUI()" method from ...

Having Trouble Retrieving and Updating Records in MS CRM?

My current situation involves working with Microsoft Dynamics coding for the first time, and I am in need of a solution for my code. I have established two entities called Acc and Con, where Acc serves as the parent entity and Con is the child entity of A ...

Tips for simplifying a JavaScript function

Hello everyone, I just joined StackOverflow and I could really use some assistance with a JavaScript and jQuery issue that I'm facing. Can someone suggest a more efficient way to write the code below?: jQuery(document).ready(function () { $("#ar ...

The ng-switch functionality does not seem to be functioning properly, even when the is

My ng-switch is displaying both buttons instead of just one when isActive is false. Can anyone help me figure out what I am doing wrong? <div ng-switch="user.IsActive"> <div ng-switch-when="false"> <button type="button" (click)="activ ...

How can a method inside an object property of the same class be invoked in JavaScript?

I'm faced with a bit of confusion here. I have a class property called hotspot of type object, declared as follows: Cannon.prototype.hotspot = {stuff: this.blah(...) }; The method blah() is actually a prototype of the same 'class': Canno ...

When using Math.floor(Math.random() * val.length), the result will be a letter rather than a number

Looking to implement a feature where a different background image is displayed randomly upon page visit or refresh. Currently, specifying the images in an array like the example below works well, but I want to be able to pull from a folder. $(document).r ...

Javascript Steps to trigger a function when selecting the Stay on Page option in Google Chrome

If you're testing my code in the Chrome Browser, hitting refresh will prompt you with 2 options. Leave This Page Stay on This Page By clicking on the Stay on this page button, it should trigger my custom function displayMsg(). Can anyone pr ...

Encountering an issue when trying to upload a file: Spring 4 and Angular JS throwing error stating that the current request is not a

Currently, I am utilizing Spring 4 along with AngularJS. To handle multipart requests, I have included commons-fileupload-1.3.2.jar and commons-io-2.4.jar in my project. However, I encountered an error stating "org.springframework.web.multipart.MultipartEx ...

Downloading files from a WebAPI using AngularJS

Searching for a way to download a file from the server using Web API and AngularJS? Below is an example of code used in an API controller. When accessing the API through a browser, the file can be successfully downloaded. However, implementing this functio ...

Efficient Ways to Present and Personalize Indian Date and Time using JavaScript

Hey there, currently creating my website and need some assistance. I'm looking to display the date and time in different sections of the page. Having some trouble with getting Indian time to work properly using PHP. If you could help me out with Ja ...

JavaScript's failure to properly handle UTF-8 encoding

Here is a snippet of code that I found on Stack Overflow and modified: <?php header('Content-Type: text/html; charset=ISO-8859-1'); $origadd = $_SESSION["OriginAdd"] // $_SESSION["OriginAdd"] contains the value "rueFrédéricMistral"; echo $o ...

Determine the index of a specific character within a string using a "for of" loop

How can I obtain the position of a character in a string when it has been separated programmatically using a for...of loop? For instance, if I wish to display the position of each character in a string with the following loop: for (let c of myString) { ...

Instructions on combining two Objects retrieved from user input's value

I have a dilemma with two JSON structures that I need to combine: First Object: {"9":{"322":{"apples":"42"}}} Second Object: {"10":{"323":{"bananas":"78"}}} The desired outcome should look like this: { "9": { "322": { "apples": " ...

Jenkins process encounters issues with sed execution

I am currently facing an issue where a script that runs successfully locally encounters difficulties during our Jenkins build process, specifically with the 'sed' command. Below is the code snippet I am using. I have double-checked the file path ...

What is the process for storing a file in a MongoDB database?

Currently, I am working on a small project where I need to store a document file in MongoDB database. I have been told that directly storing document files in MongoDB is not possible, but they can be stored in services like Google Drive or Dropbox and th ...

Display a pop-up window upon clicking anywhere on the webpage using jQuery and HTML

Is it possible for me to create a pop-up window that opens a specific website when a user clicks anywhere on the page, similar to pop-up companies? Can this be achieved using HTML, JavaScript, and jQuery? Any help would be greatly appreciated. ...

I'm attempting to store the information from fs into a variable, but I'm consistently receiving undefined as the output

I'm currently attempting to save the data that is read by fs into a variable. However, the output I am receiving is undefined. const fs = require("fs"); var storage; fs.readFile("analogData.txt", "utf8", (err, data) =&g ...