How to show ionicModal on AngularJS app start?

蓝咒 提交于 2019-12-24 13:35:15

问题


How can I show ionicModal on app start? This code doesn't work

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

but this work

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

I want open modal window on app start without delay. How can I do this? Thanks!

Edit code


回答1:


As i understand application starts when device is ready.

So you can use the $ionicPlatform.ready method to attach callbacks for when the device is 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();
  });
});



回答2:


you should wait until device is ready, there are many methods for it, here is one I found:

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


来源:https://stackoverflow.com/questions/28743467/how-to-show-ionicmodal-on-angularjs-app-start

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