2016-03-14 32 views
0

我有一个登录屏幕,我通过使用一些硬线数据进行登录。如何在angular js中自动登录?

angular.module('starter.controllers', []) 




    .controller('AppCtrl', function($scope, $rootScope, $window,    $ionicModal, $timeout, authService, $state, $http, $ionicLoading, $location) { 
     //$window.location.reload(); 

    $scope.loginSubmitted = false; 
    $scope.myflag = false; 
    $scope.User = {}; 
    $scope.toast = function() { 
     $ionicLoading.show({ 
    template: 'wrong credentials' 
}); 
$timeout(function() { 
    $ionicLoading.hide(); 
}, 1000); 
     } 
     $scope.footerflag = true; 
      $scope.hidefooter = function() { 
      $timeout(function() { 
$scope.footerflag = false; 
     },1) 
     } 
      $scope.showfooter = function() { 
    $timeout(function() { 
    $scope.footerflag = true; 
    },1) 
      } 


     $scope.doLogin = function() { 
console.log("trying login"); 

// // var res = $http.post('http://login.com/postLogin', $scope.user); 
// authService.postdetails($scope.User).success(function(data, status, headers, config) { 
// $scope.message = data; 
// console.log("succesfn"); 
// console.log(status); 
// 
// }) 
// .error(function(data, status, headers, config) { 
// alert("failure message: " + JSON.stringify({ 
//  data: data 
// })); 
// console.log(fail); 
// }); 
$scope.loginSubmitted = true; 
$scope.loginstatus = 0; 
authService.GetByUsername().success(function(data) { 
    $scope.UserData = data; 
    // console.log($scope.UserData); 
    for (var i = 0; i < $scope.UserData.length; i++) { 
    if ($scope.UserData[i].UserName == $scope.User.UserName && $scope.UserData[i].Password == $scope.User.Password) { 
     authService.currentuser = $scope.User.UserName; 
     //console.log(authService.currentuser); 
     $scope.loginstatus = 1; 
     break; 
    } 
    } 
    if ($scope.loginstatus == 1) { 

    // var lastVal = $cookieStore.get($scope.User.UserName); 
    // console.log(lastVal); 
    //$location.path('/app/playlists'); 
    $scope.loginstatus = 0; 
    $state.go('app.playlists', null, { 
     reload: true 
    }); 
    } else { 
    console.log('wrong credentials'); 
    $scope.toast(); 
    } 

}).error(function(err) { 
    console.log(err); 
}); 


     } 



     }); 

所以我想启用自动登录,直到用户点击注销按钮。如何做到这一点? 当用户名和密码匹配时,我只是重定向到另一个页面。

回答