2016-05-16 10 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: '/', 
     }); 
}); 

ログアウトコントローラ角度JS +リダイレクトが

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

を働いていない私は自分のサイトのためのルートを定義するコードの上に書かれています。ログアウトの場合、私はコントローラを "logoutCtrl"と定義しました。しかし、私のコードは動作していないようです。 SITE_URL /#/ logoutを押すと、コンソールログもlocalStorageデータも削除されません。

答えて

1

あなたは角度がそれを修正するために、コントローラに

を発射する前に確認してください(テンプレート)場合は使用しています状態のためのテンプレートlogout

持っていません:

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