Angularjs在切换路由时如何取消资源承诺

Joa*_*oao 26 angularjs

我刚刚和Angularjs擦肩而过.我有一个问题,我认为与承诺有关.

假设我加载路径'A',它通过它的控制器发出几个ajax请求:

allSites = AllSites.query({ id:categoryID });

allSites.$promise.then(function(allSites){
    //add stuff to the scope and does other things 
    //(including making another ajax request)
});
Run Code Online (Sandbox Code Playgroud)

然后我有路由'B'通过它的控制器使它自己的API请求:

$scope.categories = Category.query();
Run Code Online (Sandbox Code Playgroud)

这是路线'A'目前使用的工厂服务:

.factory('AllSites',function($resource){
    return $resource('api/categorySites/:id');
});
Run Code Online (Sandbox Code Playgroud)

当我第一次查看路线'A'但在'A'完成加载之前切换到'B'时,路线'B'坐下并等待'A'中最初请求的所有内容完成(实际上,查询()请求已经完成,但是直到来自'A'的那个,它才会解决,在那时,内部的东西.then()继续发生,即使我不需要它,因为我现在在另一条路线上.

正如您在我的devtools时间轴中看到的那样,绿线表示我何时切换到路线'B'.路由'B'的请求直到上面的两个请求(请求通常非常快)才解决.(此时我可以将视图用作用户).然后,在此之后,更多的承诺将从路线'A'解决.

我的devtools

我到处寻找答案,只能找到想要"推迟"路线加载的人,直到承诺得到解决.但就我而言,我几乎想要相反.我想在切换时杀死这些请求.

这是其他人有同样的,未回答的问题:拒绝Angularjs资源承诺

任何帮助表示赞赏.

Joa*_*oao 15

首先,我决定我需要使用,$http因为我找不到任何使用的解决方案$resource,也不能让它自己工作.

因此,这里就是我的工厂变成,基于@希德的答案在这里,使用指南,在http://www.bennadel.com/blog/2616-aborting-ajax-requests-using-http-and-angularjs.htm

.factory('AllSites',function($http,$q){

    function getSites(categoryID) {

        // The timeout property of the http request takes a deferred value
        // that will abort the underying AJAX request if / when the deferred
        // value is resolved.
        var deferredAbort  = $q.defer();

        // Initiate the AJAX request.
        var request = $http({
            method: 'get',
            url: 'api/categorySites/'+categoryID,
            timeout: deferredAbort.promise
        });

        // Rather than returning the http-promise object, we want to pipe it
        // through another promise so that we can "unwrap" the response
        // without letting the http-transport mechansim leak out of the
        // service layer.
        var promise = request.then(
            function( response ) {
                return( response.data );
            },
            function() {
                return( $q.reject( 'Something went wrong' ) );
            }
        );

        // Now that we have the promise that we're going to return to the
        // calling context, let's augment it with the abort method. Since
        // the $http service uses a deferred value for the timeout, then
        // all we have to do here is resolve the value and AngularJS will
        // abort the underlying AJAX request.
        promise.abort = function() {
            deferredAbort.resolve();
        };

        // Since we're creating functions and passing them out of scope,
        // we're creating object references that may be hard to garbage
        // collect. As such, we can perform some clean-up once we know
        // that the requests has finished.
        promise.finally(
            function() {
                promise.abort = angular.noop;
                deferredAbort = request = promise = null;
            }
        );

        return( promise );
    }

    // Return the public API.
    return({
        getSites: getSites
    });

});
Run Code Online (Sandbox Code Playgroud)

然后,在我的控制器中(从我的问题路线'A'):

var allSitesPromise = AllSites.getSites(categoryID);

$scope.$on('$destroy',function(){
    allSitesPromise.abort();
});

allSitesPromise.then(function(allSites){
    // do stuff here with the result
}
Run Code Online (Sandbox Code Playgroud)

我希望工厂不是那么凌乱,但我会拿走我能得到的东西.然而,现在有一个单独的相关问题这里,虽然承诺被取消,但下一步行动仍然延迟.如果您有答案,可以在那里发布.