0

I have written a function to open ionic popup on click of device back,problem is as soon as I click device back multiple times, the popup gets created that many times and remains in DOM. How can I close previous popup and create a new one again?

App Exit Popup:

 $rootScope.exitApp = function () {


                exitpopup = $ionicPopup.show({
                    templateUrl: 'templates/exitApp1.html'

                });

                exitpopup.then(function (res) {
                    console.log(res);

                });
                return false;
            };

RegisterBack function :

 $ionicPlatform.registerBackButtonAction(function (e) {
  // lots of  code 
 if ($ionicHistory.backView())
 $rootScope.exitApp ();
});

Am I missing something ?

1 Answers1

0

Hope below function helps you. Set flag when back button press & check flag if flag is false no need to show dialog again. if user press cancel change the flag value again.

app.run(function($ionicPlatform,$rootScope,$ionicPopup) 
{
    $rootScope.is_dialog_in_screen = false;
    $ionicPlatform.ready(function() 
    {
        if(window.cordova && window.cordova.plugins.Keyboard) 
        {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if(window.StatusBar) 
        {
            StatusBar.styleDefault();
        }
    });

    $ionicPlatform.registerBackButtonAction(function (event) 
    { 
        event.preventDefault();

        if($rootScope.is_dialog_in_screen==false)
        {
            var confirmPopup = $ionicPopup.confirm({
                title: 'Quit.?',
                template: 'Really want to exit?',
                cancelText: 'cancel',
                okText: 'ok'
            })
            .then(function(res) 
            {
                if (res) 
                {
                    alert("Exit app logic goes here");
                }
                else
                {
                    $rootScope.is_dialog_in_screen = false;
                }
            });

            $rootScope.is_dialog_in_screen = true;
        }
    }, 999);
})
Paresh Gami
  • 4,777
  • 5
  • 23
  • 41