Angular js - call a route without viewing

I am trying to call the /auth/logoutredirect URL after deleting the session:

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 really do not need a view for the AuthLogout controller, but if I do not specify templateUrlin routeProvider, I cannot make it work, and if I specify << 22> it will work.

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

+3
source share
3 answers

You can do:

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

btw may be something wrong in your code because the template works and you can use it in the same way

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

+6
source

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);
        }
    }
})

+5

use redirectTo

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

    })
});

Hope this works for you :)

+1
source

All Articles