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