2017-06-26 5 views
0

Ich bin mit angularjs Login auf meiner Website tun. Mein Code:
HTML:Cookies mit Routing angularjs

 <input type="text" ng-model="username" placeholder="Username"> 

     <input type="password" ng-model="password" placeholder="Password" > 

     <button class="btn btn-lg btn-primary btn-block" type="submit" ng-click="signin()">Sign in</button> 

JS:

app.config(function($routeProvider) { 
     $routeProvider 
      .when('/', { 
       resolve: { 
        "check": function($location, $rootScope) { 
         if(!$rootScope.valueCookie) { 
          $location.path('/login'); 
         } 
        } 
       }, 
       templateUrl: 'home.html' 
      }) 
      .when('/login', { 
       templateUrl: 'login.html' 
      }) 
      .otherwise({ 
       redirectTo: '/' 
      }); 
    }); 

$scope.signin = function() { 
     $rootScope.valueCookie = $cookieStore.put('obj', someSessionObj); 
    } 

Was ich versuche, ist entschlossen, Cookies zu tun. Wenn Cookies in einem Browser sind - nachdem Sie sich angemeldet haben und wir uns in "home.html" befinden, befindet sich nach der Refresh-Seite immer noch "home.html". Mein Code funktioniert nicht. Vielen Dank für die Antworten im Voraus.

Antwort

1

Sie setzen nur Cookies für $ rootScope.valueCookie, Sie sollten es bekommen und überprüfen, ob es existiert oder nicht. Ändern Sie Ihren Code

$cookies.put('obj', someSessionObj); 
$rootScope.valueCookie = $cookies.get('obj'); 

vom Doc: $ cookieStore ist veraltet: (seit v1.4.0)