离子空白项目弹出消息问题

Ionic blank project popup message issue

根据我的要求,我想在打开应用程序时显示弹出消息。无法使用警报。

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

但这给出了一个错误“$scope is undefined”。

$scope 未在 运行 函数中提供。因此,只能注入$rootScope到运行函数。将 $scope 替换为 $rootScope 就可以了。

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

我对你的代码做了一些改动。希望这能帮助您解决问题。