我正在我的android手机中测试一个ionic应用程序。我正在使用rootScope函数检查用户的会话,该函数在chrome调试控制台中显示TypeError("TypeError:$rootScope.checkSession不是函数")我在在线搜索中最接近的是http://www.raymondcamden.com/2014/08/16/Ionic-and-Cordovas-DeviceReady-My-Solution/但我并没有安静地理解这个概念。如果你能帮忙,我会很高兴的。谢谢
这是我的代码:
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
})
ionicPlatform.ready
事件中的代码将在Cordova的设备就绪事件触发时执行,并且$rootScope.checkSession
函数将被定义,即在Cordo娃的设备API加载并准备好访问之后。
一般来说,如果您在浏览器中运行ionic应用程序,并且您的代码可以工作,那么ionicPlatform.ready
等于window.ready
事件。但是,如果您在移动设备中运行应用程序,ionicPlatform.ready
事件将需要一段时间才能启动。由于您在ionicPlatform.ready
中定义$rootScope.checkSession
函数,因此必须仅在设备就绪事件之后调用该函数。
一个简单的解决方案是定义$rootScope.checkSession
和ionicPlatform.ready
之外的其他$rootScope函数