Angularjs Cordova/Phonegap+;角度:承诺在下一个消化周期之前不解析
我在Cordova移动应用程序中使用Angular+JQMobile,我在承诺方面遇到了一些困难。我有一个cordovaReady服务,它会返回一个承诺,我希望在承诺解决后隐藏一个splashscreen(请不要讲课)。当我尝试在不同的时间使用if-then条件来解决承诺时,技巧就出现了。我的代码:Angularjs Cordova/Phonegap+;角度:承诺在下一个消化周期之前不解析,angularjs,cordova,angularjs-scope,promise,Angularjs,Cordova,Angularjs Scope,Promise,我在Cordova移动应用程序中使用Angular+JQMobile,我在承诺方面遇到了一些困难。我有一个cordovaReady服务,它会返回一个承诺,我希望在承诺解决后隐藏一个splashscreen(请不要讲课)。当我尝试在不同的时间使用if-then条件来解决承诺时,技巧就出现了。我的代码: app.factory('cordovaReady', function($q) { var deferred = $q.defer(); return { read
app.factory('cordovaReady', function($q) {
var deferred = $q.defer();
return {
ready: function() {
document.addEventListener(
"deviceready",
function () {
deferred.resolve();
},
false
);
return deferred.promise;
}
}
});
app.run(['$rootScope', '$location', 'TagTemplates', 'cordovaReady', function($scope, $location, TagTemplates, cordovaReady) {
// If there is a user logged in, --fire camera-- and go to tag template
if ($scope.currentUser) {
// fire camera here, make sure cordova is ready first.
// alert('found user');
var promise = cordovaReady.ready();
promise.then(function() {
// alert('about to capture');
console.log('firing capture');
$scope.captureImage();
navigator.splashscreen.hide(); // this one fires.
});
console.log($scope.currentUser);
$scope.templates = $scope.currentUser.attributes.templates[0];
$scope.$apply();
$location.url("/#main");
} else {
var promise = cordovaReady.ready();
promise.then(function() {
alert('no user logged in, hide splashscreen');
navigator.splashscreen.hide(); // this one does not.
$scope.$apply(); // even with this.
});
}
}
对于现有用户,splashscreen会根据需要隐藏,我认为这是因为在if语句中发生了范围更新。但是,如果没有现有用户,splashscreen直到下一个摘要周期(单击我的应用程序中的按钮或链接)才会启动。我用警报验证了这一点
我已经尝试了各种应用范围,包括带有0延迟的setTimeout的变体,等等
有什么想法吗
谢谢。将科尔多瓦承诺转换为
$q
承诺。这些钩子在Angular的摘要周期上。我想我们需要看看处理promise的代码cordovaReady.ready()
。可能只是承诺没有得到妥善解决。谢谢你的回复。刚刚添加了cordovaReady代码。另外,您会注意到这些东西正在一个模块内运行。运行代码段。我遇到了相同的问题,对此有任何更新吗?您是否尝试过将延迟包装。在$rootScope.$apply.
中解决?