0
var weatherApp = angular.module('weatherApp', ['ngRoute', 'ngStorage']);

weatherApp.config(function ($routeProvider){
    $routeProvider
        .when('/', {
            templateUrl: 'html/views/search.html',
            controller: 'searchCtrl',
        })
        .when('/forecast',{
            templateUrl: 'html/views/forecast.html',
            controller: 'forecastCtrl',
        })
        .when('/login',{
            templateUrl: 'html/views/login.html',
            controller: 'loginCtrl',
        })
        .when('/logout', {
            controller: 'logoutCtrl',
        })
        .otherwise({
            redirectTo: '/',
        });
});

logout controller

weatherApp.controller('logoutCtrl', ["$scope", "$location", "$localStorage", function($scope, $location, $localStorage){
    $localStorage.removeItem("user_email");
    $localStorage.removeItem("user_password");
    console.log("coming in logout controller!!");
    $location.path("/login");
}]);

I have written above code to define routes for my site. For logout, I have defined controller as "logoutCtrl". But My code does not seem to work. When I hit SITE_URL/#/logout, it does not console log neither delete localStorage data.

1 Answer 1

1

You have no template for state logout

Angular uses an if (template) check before firing the controller

to fix that:

var weatherApp = angular.module('weatherApp', ['ngRoute', 'ngStorage']);

weatherApp.config(function ($routeProvider){
    $routeProvider
        .when('/', {
            templateUrl: 'html/views/search.html',
            controller: 'searchCtrl',
        })
        .when('/forecast',{
            templateUrl: 'html/views/forecast.html',
            controller: 'forecastCtrl',
        })
        .when('/login',{
            templateUrl: 'html/views/login.html',
            controller: 'loginCtrl',
        })
        .when('/logout', {
            template: " ", // <--- Notice, (" ") rather than ("")
            controller: 'logoutCtrl',
        })
        .otherwise({
            redirectTo: '/',
        });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.