2016-05-16 5 views
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-ControllerAngular JS + Redirect funktioniert nicht

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

Ich habe oben Code geschrieben Routen für meine Website zu definieren. Zum Abmelden habe ich den Controller als "logoutCtrl" definiert. Aber mein Code scheint nicht zu funktionieren. Wenn ich SITE_URL/#/logout triff, loggt es das Konsolenprotokoll nicht und löscht auch lokale Speicherdaten nicht.

Antwort

1

Sie haben keine Vorlage für Zustand logout

Angular verwendet eine if (Vorlage) überprüfen, bevor Sie den Controller Brennen

das beheben:

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: '/', 
     }); 
});