2016-10-22 4 views
-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(); 
 
    } 
 

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

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

 
\t if (window.cordova) { 
 
\t \t cordova.plugins.diagnostic.isLocationEnabled(function(enabled) { 
 
\t \t \t if(!enabled){ 
 
\t \t \t \t alert("Location is not enabled"); 
 
\t \t \t \t cordova.plugins.diagnostic.switchToLocationSettings(); 
 
\t \t \t } 
 
\t \t }, function(error) { 
 
\t \t \t alert("The following error occurred: " + error); 
 
\t \t }); 
 
\t } 
 
    }); 
 
})
しかし、これはエラー与えている "$の範囲が定義されていません"。

+0

ログキャットの詳細を確認し、ここに入力すると解決すると便利な場合があります。 – QuokMoon

答えて

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()"> 
+0

ありがとうございます。変更はしましたが、同じ問題 "$ rootScopeは未定義です" –

+0

@ChamaraMaduranga私はコードを更新しました。以前は、間違った場所に依存関係を注入してしまったことに気づきませんでした。 '$ ionicPlatform.ready'の中で' run'に注入してください – whyyie

+0

rpに感謝します。私は変更を行いました。今はエラーメッセージはありませんが、ポップアップもありません。あなたは何か考えている場合私と共有してください –

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

私はあなたのコード内のいくつかの変更を行いました。これがあなたの問題解決に役立つことを願っています。

+0

答えをありがとう。私は変更を行いました。今はエラーメッセージはありませんが、ポップアップもありません。何か考えてもらえば分かります –

+0

.run(function()の引数に$ ionicPopupを追加したことを確認してください。あなたのコードでは.ready()関数で追加されています。 –