0

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

我试图观察 $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(...) 之后立即触发(没有等待响应)

您知道响应返回后立即触发的任何事件吗?

谢谢

4

1 回答 1

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: '/'
        });
});
于 2013-09-04T10:49:53.740 回答