如何在AngularJS app上启动ionicModal?

时间:2015-02-26 13:14:55

标签: angularjs ionic-framework ionic

如何在应用启动时显示ionicModal?此代码不起作用

angular.module('testApp', ['ionic'])
.controller('MyController', function($scope, $ionicModal, $ionicPlatform) {
  $ionicPlatform.ready(function(){

    $scope.initApp = function() {
      // some init variables here
      if (someVariable){
        $scope.openModal();
      }
    }
    // init ionicModal here
    $scope.openModal = function() {
      $scope.modal.show();
    };
    $scope.initApp();
  });
});

但是这项工作

angular.module('testApp', ['ionic'])
.controller('MyController', function($scope, $ionicModal, $ionicPlatform, $timeout) {
  $ionicPlatform.ready(function(){
    $scope.initApp = function() {
      // some init variables here
      if (someVariable){
        $timeout(function() {
          $scope.openModal();
        }, 1000);
      }
    }
    // init Modal here
    $scope.openModal = function() {
      $scope.modal.show();
    };
    $scope.initApp();
  });
});

我想在app启动时打开模态窗口而不会有任何延迟。我怎样才能做到这一点?谢谢!

编辑代码

2 个答案:

答案 0 :(得分:1)

据我所知,应用程序在设备准备就绪时启动。

因此,您可以使用 $ ionicPlatform.ready 方法附加设备准备就绪时的回调。

  

设备准备就绪后触发回叫,如果设备已准备好,则立即触发回叫。 Source

angular.module('testApp', ['ionic'])
.controller('MyController', function($scope, $ionicModal, $ionicPlatform) {
  $ionicModal.fromTemplateUrl('my-modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    $scope.modal = modal;
  });
  $scope.openModal = function() {
    $scope.modal.show();
  };
  $ionicPlatform.ready(function(){
    $scope.openModal();
  });
});

答案 1 :(得分:0)

你应该等到设备准备好了,有很多方法,这里有一个我找到的:

$ionicPlatform.ready(function() {
     $scope.openModal();
});