1

I'm trying to call a function after a popup automatically closes after 1 second. This is my code:

$timeout(function() {
  var closeit = myPopup.close();
  closeit.then(function() {
    $scope.dosomething();
  });
}, 1000);

The dosomething function is never called. I'm new to AngularJS, anyone who can help me with this?

4
  • Look into promises markdalgleish.com/2013/06/using-promises-in-angularjs-views Commented Aug 17, 2015 at 18:27
  • @dfsq : myPopup is an ionicPopup. Commented Aug 17, 2015 at 18:27
  • Does myPopup return a promise? Commented Aug 17, 2015 at 18:28
  • dot syntax is reserved for objects. You can only use it on functions that return a promise or return themself Commented Aug 17, 2015 at 18:31

1 Answer 1

2

$ionicPopup - $ionicPopup.show(options) documentation:

Returns: object A promise which is resolved when the popup is closed. Has an additional close function, which can be used to programmatically close the popup.

var myPopup = show(options); // when you create a popup with $ionicPopup, you get a promise for the close event

myPopup.then(function() { // add a callback to the promise when it's fulfilled - ie the popup was closed
  $scope.dosomething();
});

$timeout(function() {
  myPopup.close();
}, 1000);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. I've tried your code, and while it does work (meaning the dosomething() function is called), the function is often called before the popup is closed. The dosomthing function actually starts a barcode scanner (cordova barcodescanner plugin). After a barcode is found and the camera closes, you can often still see the popup for a very short time before it closes.
In the $ionicPopup source code there is a small delay between the close event, and the removal of the background overlay. In addition as the promise is also used for closing the popup, your code might be running before the popup is actually closed. Without means now to reproduce it, the only solution is to fire $scope.dosomething(); inside $timeout. 0 ms delays should suffice, but you should experiment.
Thanks! I had to use a a 100ms timeout to make it work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.