سؤال

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??

هل كانت مفيدة؟

المحلول

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.org/api/ngRoute/provider/$routeProvider

نصائح أخرى

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

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

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

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 :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top