angularjs如何避免访问刚访问过的路线

时间:2013-10-01 13:13:57

标签: angularjs

说明我有一条像

这样的路线
.config(function (CONFIG,$routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: CONFIG.site.path_views + '/index/index.html',
                controller: 'IndexCtrl'
            })
            .when('/contestant/create', {
                templateUrl: CONFIG.site.path_views + '/contestant/create.html',
                controller: 'ContestantCreateCtrl'
            })
            .when('/contestant/thanks', {
                templateUrl: CONFIG.site.path_views + '/contestant/thanks.html',
                controller: 'ContestantThanksCtrl'
            })
            .when('/product', {
                templateUrl: CONFIG.site.path_views + '/product/index.html',
                controller: 'ProductCtrl'
            })
            .when('/prize', {
                templateUrl: CONFIG.site.path_views + '/prize/index.html',
                controller: 'PrizeCtrl'
            })
            .otherwise({
                redirectTo: '/'
            });

    })

我想拒绝访问或只是重定向用户  如果他/她已填写表格,请/参赛者/创建 但我找不到办法:(

1 个答案:

答案 0 :(得分:0)

app.run( function(VisitedRoutes) {
    VisitedRoutes.init();
};
app.factory('VisitedRoutes', function($rootScope,$location) {
        var routes = [];
        return {
            add : function(){
                 routes.push($location.path());
            },
            all:function(){
                return _.uniq(routes);
            },
            hasRoute:function(value){
               return _.indexOf(this.all(), value) 
            },
            init:function(){
                var that = this;
                $rootScope.$on('$routeChangeSuccess', function (scope, next, current) {
                    that.add();
                });
            }
        }
    });
app.controller('ContestantCreateCtrl',function($scope,$location,VisitedRoutes) {
    if(VisitedRoutes.hasRoute('/contestant/thanks') !== -1){
        $location.path('/');
    }
});

归功于@Jesus Rodriguez:)