[solved] Modal.show on app.run - didn't use controller in rootscope

Hey guys,

is there any option to show a modal on start?
On start i check the user status and if he/she isn’t logged in, it should show the modal for login.

Anyone knows a solution?

It’s not really a specific “option”. Just check the user status and launch modal inside of $ionicPlatform.ready . You can create and call functions in app.js. To call functions inside of app.js from an outside controller, you have to create a $rootScope function. Good luck.

1 Like

Do this on the .run(), and also do not forget to add your $ionicModal and other dependencies you need.

1 Like

So got i half … On start it shows the modal (just if he/she isn’t authorized), but now the buttons are without any function. i think it doesn’t use the controller.

https://github.com/AndreDriesel/ionic-backand-app/blob/master/www/js/app.js

How do i get the modal the use “loginCtrl”?
Before putting it into $rootScope, i got it using controller by ng-controller in < body>.
Any idea?

Just put the modal in the controller like this

> .controller('authentication', function($scope, $ionicModal){

>     console.log("I am now in the authentication mode...")

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

>   // Triggered in the login modal to close it
>   $scope.closeLogin = function() {
>     $scope.modal.hide();
>   };

>   // Open the login modal
>   $scope.login = function() {
>     $scope.modal.show();
>   };
> })
1 Like

I think it’s not that easy, I currently got the modal in my controller.
Look here:
https://github.com/AndreDriesel/ionic-backand-app/blob/master/www/js/controllers.js

The modal function needs to call a templateUrl to bind the page to the modal view like my example above.

You mean like this:

    $ionicModal.fromTemplateUrl('contact-modal.html', {
        scope: $scope,
        animation: 'slide-in-up'
    }).then(function(modal) {
        $scope.modal = modal
    })  

    $scope.openModal = function() {
        $scope.modal.show();
        console.log($scope.title);
    }

    $scope.closeModal = function() {
        $scope.modal.hide();
    };

    $scope.$on('$destroy', function() {
        $scope.modal.remove();
    });

???

1 Like

Yes man, did it work??

No, because I still had the modal in my controller. You haven’t clicked at my github link and looked into the complete code, right?

The modal work on every site. But not when i open it on start via rotoscope.

//Edit
Got it!
Now everything works fine.

What have i done?

  1. checked authentication in rootscope
  2. if auth failed, than $rootScope.$broadcast(‘app.loggedOut’);
  3. build global controller (LoginCtrl) with modal and implemented it into body via
    < body ng-app=“SimpleRESTIonic” ng-controller=“LoginCtrl as login”>
  4. build function in global controller:
    $scope.$on(‘app.loggedOut’, function(e) {
    console.log(‘unauthorized user - open modal’);
    $scope.modal.show();
    });

see complete code here:
https://github.com/AndreDriesel/ionic-backand-app

Question:
How do i format text into code in my posts? I use a Macbook Air.