Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

angularjs - ui-router resolve behaves strangely in Ionic

I started from a demo Ionic app (ionic start myApp sidemenu), and added a resolve to one of the views:

resolve: {
  issue: function($q, $timeout) {
    var defer = $q.defer();
    //defer.reject();       // Doesn't work browser or device
    $timeout(defer.reject); // Works in browser, but not device
    return defer.promise;
  }
}

I monitor rejected resolves here:

.run(function($ionicPlatform, $rootScope, $ionicLoading) {
  $ionicPlatform.ready(function() {
    // regular stuff here

    $rootScope.$on('$stateChangeError', function() {
      $ionicLoading.show({
        template: 'All good!'
      });
    });
  });
});

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!

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?

SEE HERE HOW TO REPRODUCE THE ISSUE

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

1) $timeout needs to be more than 200

First off - I have never seen constructs like $timeout(deferred.reject); I don't believe defered.reject returns any function that $timeout expects.

I tried 50 and it works. Here is http://plnkr.co/edit/6NXZEXrfz3WHVqoaRYJB?p=preview with timeout 50 or less but works i.e. All Good! is printed.

resolve: {
              test: function($q,  $timeout) {
                var defer = $q.defer(); 
                $timeout(function(){
                  defer.reject();
                },50);
                return defer.promise; 
              }
}

This is with stateChangeError outside the iconicPlatform.ready

2) $stateChangeError not working

I am thinking that this has to do with the timings. I don;t know the internals as to when the ionicPlatform.ready() gets executed but I am not sure if it is even necessary. Your starter module depends on it so it will be loaded. Sometimes seed projects have more than 'seed'.

UPDATED - Found a bug with respect to ready() on other devices. It seems that ready method is not getting executed.

https://github.com/driftyco/ionic/issues/1751


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...