Delaying AngularJS route change until model loaded to prevent flicker

后端 未结 13 2294
误落风尘
误落风尘 2020-11-22 02:28

I am wondering if there is a way (similar to Gmail) for AngularJS to delay showing a new route until after each model and its data has been fetched using it

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:23

    I worked from Misko's code above and this is what I've done with it. This is a more current solution since $defer has been changed to $timeout. Substituting $timeout however will wait for the timeout period (in Misko's code, 1 second), then return the data hoping it's resolved in time. With this way, it returns asap.

    function PhoneListCtrl($scope, phones) {
      $scope.phones = phones;
      $scope.orderProp = 'age';
    }
    
    PhoneListCtrl.resolve = {
    
      phones: function($q, Phone) {
        var deferred = $q.defer();
    
        Phone.query(function(phones) {
            deferred.resolve(phones);
        });
    
        return deferred.promise;
      }
    }
    

提交回复
热议问题