-1

As per the my requirement i want to show a popup message when opening the App. can't use alert.

angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function($scope, $ionicPopup, $timeout) {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }

  $scope.showAlert = function() {
    var alertPopup = $ionicPopup.alert({
   title: 'Don\'t eat that!',
   template: 'It might taste good'
    });

    alertPopup.then(function(res) {
   console.log('Thank you for not eating my delicious ice cream cone');
    });
 };

 if (window.cordova) {
  cordova.plugins.diagnostic.isLocationEnabled(function(enabled) {
   if(!enabled){
    alert("Location is not enabled");
    cordova.plugins.diagnostic.switchToLocationSettings();
   }
  }, function(error) {
   alert("The following error occurred: " + error);
  });
 }
  });
})

But this is giving an error " $scope is undefined".

sebaferreras
  • 44,206
  • 11
  • 116
  • 134

2 Answers2

2

$scope is not provided in run function. Therefore, you can only inject $rootScope to run function. Replace the $scope with $rootScope and you will be doing fine.

.run(function($ionicPlatform, $rootScope, $ionicPopup, $timeout) {
    $ionicPlatform.ready(function() {

    // Code here
    ....

    $rootScope.showAlert = function() {
       var alertPopup = $ionicPopup.alert({
         title: 'Don\'t eat that!',
         template: 'It might taste good'
       });

       alertPopup.then(function(res) {
         console.log('Thank you for not eating my delicious ice cream cone');
       });
    };

    // Code here
    ....
});

<button ng-click="$root.showAlert()">
whyyie
  • 792
  • 3
  • 10
  • Thanks. Did the changes , but still same issue "$rootScope is undefined" – Chamara Maduranga Oct 22 '16 at 06:01
  • @ChamaraMaduranga i have updated the code. Previously i didnt notice that you inject the dependency in wrong location. Please inject into `run` instead inside of `$ionicPlatform.ready` – whyyie Oct 22 '16 at 09:51
  • Thanks for the rp. I did the changes. Now there is no error message but popup also not there. If you have any idea please share with me – Chamara Maduranga Oct 23 '16 at 14:14
  • @ChamaraMaduranga which popup do you want to show? if you want to show the ionic popup straight away when app launch you need to initialise `$rootScope.showAlert()` in your controller. – whyyie Oct 25 '16 at 14:01
1
angular.module('starter', ['ionic'])

.run(function($ionicPlatform, $rootScope, $ionicPopup, $timeout) {
  $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();
    }

     $rootScope.showAlert = function() {
       var alertPopup = $ionicPopup.alert({
         title: 'Don\'t eat that!',
         template: 'It might taste good'
       });

       alertPopup.then(function(res) {
         console.log('Thank you for not eating my delicious ice cream cone');
       });
    };

    if (window.cordova) {
        cordova.plugins.diagnostic.isLocationEnabled(function(enabled) {
            if(!enabled){
                alert("Location is not enabled");
                cordova.plugins.diagnostic.switchToLocationSettings();
            }
        }, function(error) {
            alert("The following error occurred: " + error);
        });
    }
  });
})

I have made few changes in your code. Hope this will help you resolving your issue.

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57