如何在 AngularJS 应用启动时显示 ionicModal?

How to show ionicModal on AngularJS app start?

如何在应用程序启动时显示 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();
  });
});

我想在应用程序启动时立即打开模式 window。我怎样才能做到这一点?谢谢!

编辑代码

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

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

据我了解,应用程序会在设备准备就绪时启动。

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

Trigger a callback once the device is ready, or immediately if the device is already 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();
  });
});