How to call modal.show on controller init?

I’m using the ionic sidemenu template. Everything works except when I put modal.show inside the AppCtrl controller an undefined error was thrown. Basically what I want to do is on startup check if a user is already logged in, if not, show the login modal. Here’s my code:

.controller('AppCtrl', function ($scope, $ionicModal, $timeout, $ionicLoading) {
    // Form data for the login modal
    $scope.loginData = {};
    $scope.currentUser = {};

    // Ionic loading screen
    $scope.showLoading = function () {...};
    $scope.hideLoading = function () {...};

// Create the login & register modal that we will use later
$ionicModal.fromTemplateUrl('templates/login.html', {
    scope: $scope
    }).then(function (modal) {
        $scope.modal = modal;
    });

// Show/close login form modal
$scope.closeLogin = function () {
    $scope.modal.hide();
};
$scope.login = function () {
    $scope.modal.show();
};

// Log out the current user
$scope.logout = function () {...};

// Perform the login action when the user submits the login form
$scope.doLogin = function () {...};

// Send SMS for verification
$scope.doSendSMSCode = function () {...};

// Perform user registration action
$scope.doReg = function () {...};

// Check if currentUser exists if not show sign in page. THIS IS WHERE THE ERROR IS THROWN
showLoginOnNullUser = function () {
    if (!$scope.currentUser)
        $scope.login();
};
showLoginOnNullUser();
})

Now the error console output is:

Error: 'undefined' is not an object (evaluating '$scope.modal.show')

You might be trying to show the modal before it has finished loading.
May be try moving your showLoginOnNullUser() call to inside your modal loading callback:

$ionicModal.fromTemplateUrl('templates/login.html', {
    scope: $scope
    }).then(function (modal) {
        $scope.modal = modal;
        showLoginOnNullUser();
    });

works like a charm. Thanks a lot!

yeah! its working but it shows parent view first and then modal animate from bottom. What I want that modal already open when App start without showing the parent view.

It probably shouldn’t be a modal then and just it’s own view.