2016-04-12 8 views
0

Ich teste eine ionische App in meinem Android-Handy. Ich verwende eine rootScope-Funktion, um die Sitzung eines Benutzers zu überprüfen, und diese Funktion zeigt einen TypeError in der Chrome-Debug-Konsole an ("TypeError: $ rootScope.checkSession ist keine Funktion") http://www.raymondcamden.com/2014/08/16/Ionic-and-Cordovas-DeviceReady-My-Solution/ aber ich habe nicht ruhig das Konzept bekommen. Ich würde mich freuen, wenn du helfen könntest. Vielen Dank!rootScope-Funktionen im .run-Modul werden nicht erkannt

Hier ist mein Code:

forkapp.run(function($ionicPlatform, $rootScope, $firebaseAuth, $firebase, $window, $ionicLoading) { 
    $ionicPlatform.ready(function() { 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
    // for form inputs) 
    var fb = new Firebase("https://glowing-torch-9862.firebaseio.com/"); 
    if (window.cordova && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
    } 
    if (window.StatusBar) { 
     StatusBar.styleDefault(); 
    } 

    $rootScope.userEmail = null; 
    $rootScope.baseUrl = 'https://glowing-torch-9862.firebaseio.com/'; 

    var authRef = new Firebase($rootScope.baseUrl); 
    $rootScope.auth = $firebaseAuth(authRef); 

    $rootScope.show = function(text) { 
     $rootScope.loading = $ionicLoading.show({ 
     content: text ? text : 'Loading..', 
     animation: 'fade-in', 
     showBackdrop: true, 
     maxWidth: 200, 
     showDelay: 0 
     }); 
    }; 

    $rootScope.hide = function() { 
     $ionicLoading.hide(); 
    }; 

    $rootScope.notify = function(text) { 
     $rootScope.show(text); 
     $window.setTimeout(function() { 
     $rootScope.hide(); 
     }, 1999); 
    }; 

    $rootScope.logout = function() { 
     $rootScope.auth.$logout(); 
     $rootScope.checkSession(); 
    }; 

    $rootScope.checkSession = function() { 
     var auth = new FirebaseSimpleLogin(authRef, function(error, user) { 
     if (error) { 
      // no action yet.. redirect to default route 
      $rootScope.userEmail = null; 
      $window.location.href = '#/auth/signin'; 
     } else if (user) { 
      // user authenticated with Firebase 
      $rootScope.userEmail = user.email; 
      $window.location.href = ('#/event'); 
     } else { 
      // user is logged out 
      $rootScope.userEmail = null; 
      $window.location.href = '#/auth/signin'; 
     } 
     }); 
    }; 
    }); //ionic platform ready 
    }) 
+0

sein könnte, weil '$ rootScope.checkSession();' in 'logout' verwendet wird, bevor es definiert wurde. – Daniel

Antwort

0

Ihr Code in ionicPlatform.ready Ereignis ausgeführt wird und $rootScope.checkSession Funktion definiert werden, wenn Cordova deviceready Ereignis ausgelöst wird, das heißt, nach Cordova Gerät APIs geladen haben und sind für den Zugriff bereit.

Im Allgemeinen ist ionicPlatform.ready gleich window.ready Ereignis, wenn Sie Ihre ionische App im Browser ausführen und Ihr Code wird funktionieren. Es dauert jedoch einige Zeit, bis das Ereignis ionicPlatform.ready ausgelöst wird, wenn Sie Ihre App auf einem Mobilgerät ausführen. Da Sie die Funktion $rootScope.checkSession in ionicPlatform.ready definieren, müssen Sie diese Funktion nur nach dem Ereignis "deviceready" aufrufen.

Eine einfache Lösung ist definiert $rootScope.checkSession und andere $ rootScope Funktionen außerhalb von ionicPlatform.ready

Verwandte Themen