Ionic空白项目弹出消息问题



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

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在run函数中没有提供。因此,只能注入$rootScope来运行函数。用$rootScope代替$scope,你会做得很好。

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

我对你的代码做了一些修改。

相关内容

  • 没有找到相关文章

最新更新