离子根据一些变量调用3个弹出窗口中的一个

时间:2015-09-14 21:33:31

标签: javascript angularjs popup ionic-framework ionic

我的Ionic应用程序中有3个弹出窗口;

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',
      });
    };
  });

我想将它们分配到一个按钮。单击按钮时

var x=1我想致电showAlert1

var x=2我想致电showAlert2

var x=3我想致电showAlert3

有可能吗?

修改

我的index.html结构:

<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 个答案:

答案 0 :(得分:0)

所有警报都是相同的,除了标题(或模板,如果你正在使用该选项),所以不要在单独的函数中调用$ ionicPopup.alert()调用,只需调用一个函数,并将条件放在标题上和这样的模板

 $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
      });
  };