问题描述
我从一个演示离子应用程序开始( ionic start myApp sidemenu
),
For some reason, if resolve
rejects immediately (see defer.reject()
above), the callback of $stateChangeError
is not run. If I do exactly the same, but outside of Ionic, it works!
此外,尝试通过 $ timeout(defer.reject); 延迟
解决
拒绝code>导致不同的行为。现在它按预期在浏览器中工作,但仍然无法在设备上运行。试图延迟甚至更多,导致设备成功:
Moreover, trying to delay the resolve
rejection by doing $timeout(defer.reject);
results in a different behaviour. Now it works in a browser as expected, but still doesn't work on a device. Trying to delay even more, results in success on device:
$timeout(function() {
defer.reject();
}, 250); // Doesn't work for me with 200 or less
任何人都可以了解这一点吗?
Can anyone shed light on this?
推荐答案
根据我对Angular和promise模型的体验。为了解决/拒绝一个承诺,Angular必须勾选JS事件循环的一个循环 - nextTick - 这可以使用$ scope.apply()完成,这就是我们在单元测试中模拟这些东西的方式。
From my experience with Angular and the promise model. In order to resolve/reject a promise Angular must tick over one cycle of the JS event loop - nextTick - and this can be accomplished using $scope.apply() which is how we mock such things in unit tests.
这是一个很棒的。$ evalAsync - 从我可以收集到的$ timeout是在下一个tick中评估函数。这个代码按照您概述的方式工作的原因是什么。
This is an awesome article that talks about $timeout and $scope.$evalAsync - from what I can gather $timeout is evaluating the function in the next tick. Which is the reason that this code works the way you outlined.
resolve: {
issue: function($q, $timeout) {
var defer = $q.defer();
//defer.reject(); // <---- no nextTick
$timeout(defer.reject); // <---- $timeout evaluates on nextTick
return defer.promise;
}
}
这是另一个,讨论了$ q的textTick实现。
This is another article that talks about the textTick implementation of $q.
我知道这并不能解决你的问题 - 但它应该说清楚为什么会发生这种情况!祝你好运!
I know that this doesnt solve your problem - but it should shed some light onto why this is happening! Good luck!
这篇关于ui-router resolve在Ionic中表现得很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!