2016-05-16 53 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

角度使用if(模板)發射控制器之前檢查

來修復:

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