Aborting ngResource using a promise object

前端 未结 3 1052
再見小時候
再見小時候 2021-01-19 08:16

I\'ve recently learned that ngResource request can be aborted either by specifying a timeout in ms or passing a deferred object.

The second solution does not seem to

3条回答
  •  忘掉有多难
    2021-01-19 08:36

    Try using $timeout, instead of setTimeout, since that will take care of making sure your resolve is captured by angular $digest cycle.

    myApp.controller('MyCtrl', function($scope, $q, $log, $timeout, myResource) {
        var aborter = $q.defer();
        $timeout(function() {
           $log.info('Aborting...');
           aborter.resolve();
        }, 10);
        myResource.getResource(aborter).query().$promise.then(function(data) {
            $scope.data = data;
        });
    });
    

提交回复
热议问题