Ionic calling one of 3 popups depending on some variable

可紊 提交于 2020-01-06 18:31:28

问题


I have 3 popups in my Ionic app;

angular.module('starter')
  .controller('PopupCtrl', function($scope, $ionicPopup, $timeout) {
    $scope.showAlert1 = function() {
      var alertPopup = $ionicPopup.alert({
        title: 'Succes!',
      });
    };
    $scope.showAlert2 = function() {
      var alertPopup = $ionicPopup.alert({
        title: 'Fail...',
      });
    };
    $scope.showAlert3 = function() {
      var alertPopup = $ionicPopup.alert({
        title: 'Third option',
      });
    };
  });

I'd like to assign them to one button. When the button is clicked and

var x=1 i'd like to call showAlert1

var x=2 i'd like to call showAlert2

var x=3 i'd like to call showAlert3

Is it possible?

EDIT:

My index.html structure:

<body ng-app="starter" ng-controller="PopupCtrl">

  <ion-nav-bar class="bar-positive" align-title="center">
  </ion-nav-bar>

  <ion-nav-view class="slide-left-right"></ion-nav-view>

  <script id="list.html" type="text/ng-template">
    <ion-view title="MyApp">
      <ion-nav-buttons side="right">
        <button class="button button-icon icon ion-android-locate" ng-click="showAlert()" onclick="writeText()"></button>
      </ion-nav-buttons>

      <ion-content>
        <ul class="list">
            <!--some list-->
        </ul>
      </ion-content>
    </ion-view>

    <!--some subpages templates-->

</body>

回答1:


All of your alerts are same except title (or template, if you are using that option), so instead of calling $ionicPopup.alert() call in separate functions just make one function to call that, and put condition on title and template like this

 $scope.showAlert = function() {
      var myTitle = "";
      if(x==1){
        title = "Succes!";
     } else if(x==2){ 
        title = "Fail!"; 
     } else { title = "third option"; }
      var alertPopup = $ionicPopup.alert({
        title: myTitle
      });
  };


来源:https://stackoverflow.com/questions/32574301/ionic-calling-one-of-3-popups-depending-on-some-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!