admin管理员组

文章数量:1290228

I'm trying calling the /auth/logout url to get redirected after session is deleted:

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('/');
}]);

I actually don't need a view for AuthLogout controller but if I do not specify the templateUrl in routeProvider I can't get this to work, while if I specify a templateUrl it works.

How can I call the url/controller without to having to load a view??

I'm trying calling the /auth/logout url to get redirected after session is deleted:

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('/');
}]);

I actually don't need a view for AuthLogout controller but if I do not specify the templateUrl in routeProvider I can't get this to work, while if I specify a templateUrl it works.

How can I call the url/controller without to having to load a view??

Share Improve this question edited Feb 18, 2014 at 19:15 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Feb 18, 2014 at 11:32 Filippo orettiFilippo oretti 49.9k96 gold badges229 silver badges351 bronze badges 3
  • Try template:'' a little tricky but it should work ^^ – Whisher Commented Feb 18, 2014 at 11:40
  • i get a an infinite loop and the browser crashes with this :O – Filippo oretti Commented Feb 18, 2014 at 11:47
  • this is strange in the when block you can use or template or templateUrl so the infinite loop I think it's not relate. – Whisher Commented Feb 18, 2014 at 11:53
Add a ment  | 

3 Answers 3

Reset to default 6

You could do :

.when('/auth/logout', {
    controller: function(){
         //do staff
    }
})

btw may be there is something wrong in your code because template works and you could exploit it in the same way

http://docs.angularjs/api/ngRoute/provider/$routeProvider

You can use a resolve handler according to the post https://github./angular/angular.js/issues/1838

Checkout this quick example and notice the alert statement in resolve.

http://jsfiddle/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);
        }
    }
})

Instead of the alert you can have your own statements

use redirectTo

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

    })
});

Hope this will work for you :)

本文标签: javascriptAngular jscall a route without viewStack Overflow