-1

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

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 未定义”。

4

2 回答 2

2

$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()">
于 2016-10-22T03:36:09.880 回答
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);
        });
    }
  });
})

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

于 2016-10-22T07:02:04.693 回答