Close custom templateURL popup

Hello, my question is probably simple : I want, when the user click in button inside the template, the pop up close. I’ve actually use this (My question is a copie of this question) :

My Controller JS :

  $scope.showAlert = function() {
   var alertPopup = $ionicPopup.alert({
     templateUrl: 'templates/popup.html'
   });
   alertPopup.then(function(res) {
     console.log('Thank you for not eating my delicious ice cream cone');
   });

 };

var alertPopup;

 $scope.sendOrder = function() {
  alertPopup.close();
};

With this in my button in template HTML :

<a class="button button-full " style="font-weight: bolder;" id="bwlogin" ng-click="sendOrder()">Fermer
</a>

But I’ve this error :

TypeError: Cannot read property ‘close’ of undefined

at Scope.$scope.sendOrder (controller.js:224)
at $parseFunctionCall (ionic.bundle.js:21037)
at ionic.bundle.js:53344
at Scope.$get.Scope.$eval (ionic.bundle.js:23093)
at Scope.$get.Scope.$apply (ionic.bundle.js:23192)
at HTMLAnchorElement. (ionic.bundle.js:53343)
at HTMLAnchorElement.eventHandler (ionic.bundle.js:11706)
at triggerMouseEvent (ionic.bundle.js:2863)
at tapClick (ionic.bundle.js:2852)
at HTMLDocument.tapMouseUp (ionic.bundle.js:2925)(anonymous function) @ >>ionic.bundle.js:20299$get @ ionic.bundle.js:17249$get.Scope.$apply @ >>ionic.bundle.js:23194(anonymous function) @ ionic.bundle.js:53343eventHandler @ >>ionic.bundle.js:11706triggerMouseEvent @ ionic.bundle.js:2863tapClick @ >>ionic.bundle.js:2852tapMouseUp @ ionic.bundle.js:2925

Anyone can help me ? Thanks for your time !

Problem is in your variable alertPopup scope. You can’t have it defined in so many places and expect everything will work just fine.

You should read this excellent StackOverflow article: http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript it will give you much better perspective on how JavaScript variable scope works.

2 Likes

Thank you for your answer.
But can you give me an indication in my case ? Thank you in advance.

1 Like

I already told you, you’re trying ti define alertPopup in several places and that’s causing problems with variable scope.

Spend some looking at StackOverflow article I gave you in my previous post, it will give you a much better perspective on how JavaScript scope works.