2014-02-18 87 views
3

我试图调用/auth/logout URL会被删除后重定向:角JS - 呼叫路由,而不视图

app.config(['$routeProvider',function($routeProvider) { 
    $routeProvider 
    .when('/auth/logout',{ 
     controller:'AuthLogout' 
     //templateUrl: not needed 

    }) 
}) 
.controller('AuthLogout', ['$window','$location', function ($window,$location) { 
    $window.localStorage.removeItem('user_username'); 
    $window.localStorage.removeItem('user_id'); 
    $window.localStorage.removeItem('user_session_token'); 
    $location.path('/'); 
}]); 

我其实并不需要AuthLogout控制器的视图,但如果我不这么做在routeProvider中指定templateUrl我无法得到此工作,而如果我指定templateUrl它的作品。

如何在不需要加载视图的情况下调用url/controller?

+0

尝试模板警报:“”有点棘手,但它应该工作^^ – Whisher

+0

我得到一个无限循环和浏览器崩溃与此:O – sbaaaang

+0

这是奇怪的,当你可以使用块或模板或templateUrl,所以无限循环,我认为它不相关。 – Whisher

回答

1

使用redirectTo

app.config(['$routeProvider',function($routeProvider) { 
    $routeProvider 
    .when('/auth/logout',{ 
     redirectTo:'/' 

    }) 
}); 

希望这会为你工作:)

+0

尝试它不确定它有助于我需要运行AthLogout控制器,然后重定向... – sbaaaang

5

您可以使用一个决心处理器根据岗位https://github.com/angular/angular.js/issues/1838

结帐这个简单的例子,并注意解决警报语句。

http://jsfiddle.net/Wk7WD/34/

.when('/detail/:id/', { 
    resolve: { 
     load: function ($route, dataService) { 
      alert("hello"); 
      //Your statements instead of all this which I found in an example 
      return dataService.load($route.current.params.id); 
     } 
    } 
}) 

相反,你可以有你自己的声明