AngularJs超时响应时间长

时间:2013-09-04 10:27:41

标签: angularjs time error-handling timeout request

如果后端调用需要更长的时间,我会创建一个显示消息的函数...

我试图唤醒$ locationChangeSuccess事件......

        $scope.$on('$locationChangeStart', function (event, newLoc, oldLoc){
           deferred = $timeout(function() {
                    alert('takes more that 10 sec!!!');
                }, 10000);
        });


    $scope.$on('$locationChangeSuccess', function (event, newLoc, oldLoc){
           console.log('Cancelled: ' + $timeout.cancel(deferred));
        });

它没有用,因为$ locationChangeSuccess事件在$ location.path(...)之后立即触发(没有等待响应)

你知道在响应返回后发生的任何事件吗?

感谢

1 个答案:

答案 0 :(得分:0)

如果我理解正确的话:

app.controller('LocalCtrl', function ($scope, $timeout, $q) {

    var defer = $q.defer();

    defer.promise.then(function () {
        alert('End request.');
    });

    $timeout(function () {
        defer.resolve();
    }, 2000);

});

或使用路由配置:

app.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
              templateUrl: 'views/main.html'
            , controller: 'MainCtrl'
        })
        .when('/test', {
              templateUrl: 'views/local.html'
            , controller: 'LocalCtrl'
            , resolve: {
                app: function ($q, $timeout) {
                    var defer = $q.defer();
                    $timeout(function () {
                        defer.resolve();
                    }, 2000);

                    return defer.promise;
                }
            }
        })
        .otherwise({
            redirectTo: '/'
        });
});
相关问题