【问题标题】:Angular js - stop Controller's activities (http request tho) when switching controller (site page)Angular js - 切换控制器(站点页面)时停止控制器的活动(http请求)
【发布时间】:2014-02-12 14:25:37
【问题描述】:

在我的应用中一切都很好,但是

我遇到了一个奇怪的问题:

如果我在/channels 并启动pagination,则分页需要大约 5 秒才能结束,

所以如果在分页加载的同时,我 更改控制器并查看 浏览另一个应用程序页面,当分页完成他的过程时,我会被重定向到 /channels :哦

这怎么可能?以及如何避免这种情况?

这是一段简单的代码:

 $routeProvider
        .when('/',{
            templateUrl:'views/home/index.html',
            controller:'HomeController'
        })
        .when('/auth/login',{
            templateUrl:'views/auth/login.html',
            controller:'UsersController'
        })
        .when('/auth/signup',{
            templateUrl:'views/auth/signup.html',
            controller:'UsersController'
        })
        .when('/channels',{
            templateUrl:'views/channels/index.html',
            controller:'BrowseController'
        })
        .when('/channels/search',{
            templateUrl:'views/channels/index.html',
            controller:'BrowseController',
            reloadOnSearch: false
        })
        .when('/channels/create',{
            templateUrl:'views/channels/create.html',
            controller:'ChannelsController'
        })
        .when('/users',{
            templateUrl:'views/users/index.html',
            controller:'UsersController'
        })
        .otherwise({
            redirectTo:'/'
        });

    app.controller('BrowseController',['$rootScope','$scope','$route','$routeParams','$http','$location','ngDialog', function($rootScope,$scope,$route,$routeParams,$http,$location,ngDialog) {
          $scope.layout.loading = true;
        $scope.pagination = {
            "keywords":$routeParams.keywords,
            "offset":$routeParams.offset ,
            "genre":$routeParams.genre ,
            "last_item":0,
            "results": []
        }

        $scope.redirect_pagination = function(_map){

            $location.path('channels/search').search(_map);

        }

        $scope.paginate = function(_offset,_genre,_keywords){
             $rootScope.layout.loading = true;
            if(!_offset){
                _offset = 1;
            }

            $http({
                method:'GET',
                url:$scope.config.app_ws+'get/channels/',
                params:{
                    offset:_offset,
                    genre:_genre,
                    keywords: _keywords
                }
            }).success(function(response){
            $scope.pagination.results.push(response);
            $scope.pagination.last_item = $scope.pagination.results.length;
            if($scope.config.app_url !== '/channels'){
                 $scope.redirect_pagination({
                    'offset':_offset,
                    'genre':_genre,
                    'keywords':_keywords
                });
             }

             $scope.pagination.offset = _offset * 2 ;
             $rootScope.layout.loading = false;
            }).error(function(status, response){
                alert('opsss');
            });
        }
        //default pagination or by url params

        $scope.paginate($scope.pagination.offset,$scope.pagination.genre,$scope.pagination.keywords);

    }])

其他东西:

$rootScope.$on('$routeChangeStart', function(newRoute, oldRoute) {
$rootScope.layout.loading = true;

//Make app scroll to top by default while navigating or use #anchor in url like http://sitse.com/someroute/?scrollTo=<:id_element:> to make it scroll to that element :id:
$location.hash($routeParams.scrollTo);
$anchorScroll();
//Close dialog every route change
ngDialog.close();

});
$rootScope.$on('$routeChangeError', function() {
//hide loading gif
$rootScope.layout.loading = false;
});
$rootScope.$on('$routeChangeSuccess', function() {
//Animate only some route
if($rootScope.layout.routes.indexOf($rootScope.config.app_url) == -1){
    $rootScope.layout.animation = false;
}else{
    $rootScope.layout.animation = true;
} 
//hide loading gif
$rootScope.layout.loading = false;

});

}]);

【问题讨论】:

  • 只是澄清一下,当用户请求特定路由时,您希望重定向它们,直到另一个请求完成,然后将它们重定向回来?
  • @MattWay no no 我想避免这种情况:) 因为实际上如果分页 ajax 没有完成并且 iswitched 控制器(网站页面),我会被重定向
  • 听起来你可能想做一个resolvedocs.angularjs.org/api/ngRoute.$routeProvider
  • @MattWay 实际上我的梦想是在我浏览新控制器时停止前一个控制器上的任何活动(我认为这是 Angular 中的默认内容:():P 或至少停止 http 请求以前的控制器可能没问题
  • @MattWay 您将如何在此代码中实现解析?我无法全部了解我是角度的新手:/

标签: javascript angularjs http redirect controller


【解决方案1】:

您需要一种方法来阻止$location.path() 将用户重定向回来。我能想到的一种方法是设置一个标志,如下所示:

app.controller('BrowseController', [..., function($scope, ...) {
    ...

    var paginationInProgress = false;

    // Cancel pagination if route has been changed
    $scope.$on('$routeChangeStart', function() {
        paginationInProgress = false;
    });

    $scope.redirect_pagination = function(_map){
        if (paginationInProgress) {
            $location.path('channels/search').search(_map);
            paginationInProgress = false;
        }
    };

    $scope.paginate = function(_offset,_genre,_keywords){
        ...
        paginationInProgress = true;
    };
}]);

【讨论】:

  • 这是一个聪明的解决方案谢谢你我会等待一个更好的解决方案,如果不是我会接受你自己的:)
  • @sbaaaang 没问题 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 2013-06-12
  • 2013-09-10
相关资源
最近更新 更多