离子和Angular JS - 关闭自定义弹出窗口

时间:2015-07-16 19:20:35

标签: angularjs popup ionic-framework ionic

我的问题可能很简单:我想,当用户点击模板内的按钮时,弹出关闭。 我实际上是用这个:

我的控制器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();
};

在模板HTML中使用我的按钮:

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

但我发现了这个错误:

>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.<anonymous> (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

任何人都可以帮助我吗?谢谢你的时间!

3 个答案:

答案 0 :(得分:2)

在您的代码中,第一个alertPopup变量超出了sendOrder函数的范围。 调用sendOrder时。该函数使用

的未定义值
var alertPopup;

这应该有效:

var alertPopup; 

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

     };

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

以身作则: http://jsbin.com/xuvefatoha/edit?html,js,console,output

答案 1 :(得分:1)

您应该将弹出窗口存储为$ scope变量。类似的东西:

$scope.alert = $ionicPopup.show(...);

因此,您可以稍后访问它:

$scope.alert.close();

工作示例:http://play.ionic.io/app/ad238765d6d2

答案 2 :(得分:0)

请注意,这不起作用:

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

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

$ionicPopup.alert(...)返回我们想要的内容,而$ionicPopup.alert(...).then(...)返回类似的内容,但没有close函数。 花了一段时间才得到它...