2016-11-25 197 views
-2

這是我的問題。新的angularjs和我想獲得登錄用戶的信息。所以我可以例如顯示它。我不知道如何或從哪裏開始從這裏是我的主角控制器。我想獲得登錄用戶信息

var myApp = angular.module('myApp', ['ngResource', 'ngRoute']); 

myApp.config(function ($routeProvider) { 
    $routeProvider 
    .when('/', { 
     templateUrl: 'partials/main.html', 
     access: {restricted: true} 
    }) 
    .when('/api/meetups', { 
     templateUrl: 'partials/main.html', 
     access: {restricted: true} 
    }) 
    .when('/login', { 
     templateUrl: 'partials/login.html', 
     controller: 'loginController', 
     access: {restricted: false} 
    }) 
    .when('/prive', { 
     templateUrl: 'partials/prive.html', 
     controller: 'userController', 
     access: {restricted: true} 
    }) 
    .when('/logout', { 
     controller: 'logoutController', 
     access: {restricted: true} 
    }) 
    .when('/register', { 
     templateUrl: 'partials/register.html', 
     controller: 'registerController', 
     access: {restricted: false} 
    }) 
    .when('/one', { 
     template: '<h1>This is page one!</h1>', 
     access: {restricted: true} 
    }) 
    .when('/two', { 
     template: '<h1>This is page two!</h1>', 
     access: {restricted: false} 
    }) 
    .otherwise({ 
     redirectTo: '/' 
    }); 
}); 

myApp.run(function ($rootScope, $location, $route, AuthService) { 
    $rootScope.$on('$routeChangeStart', 
    function (event, next, current) { 
     AuthService.getUserStatus() 
     .then(function(){ 
     if (next.access.restricted && !AuthService.isLoggedIn()){ 
      $location.path('/login'); 
      $route.reload(); 
     } 
     }); 
    }); 
}); 


myApp.controller('meetupsController', ['$scope', '$resource', 'AuthService', function ($scope, $resource, AuthService) { 
    var Meetup = $resource('/api/meetups'); 
$scope.meetups = [] 

    Meetup.query(function (results) { 
    $scope.meetups = results; 
    }); 



    $scope.createMeetup = function() { 
    var meetup = new Meetup(); 
    meetup.name = $scope.meetupName; 
    meetup.text = $scope.username; 
    meetup.$save(function (result) { 
     $scope.meetups.push(result); 
     $scope.meetupName = ''; 
     $scope.username = ''; 
    }); 
    } 





}]); 
myApp.controller('userController', ['$scope', '$resource', function ($scope, $resource) { 
/* var Meetup = $resource('/api/user'); 
$scope.users = [] 

    Meetup.query(function (results) { 
    $scope.users = results; 
    }); 
*/ 

var Meetup = $resource('/api/user', {},{ 
    query: {method: 'get', isArray: true} 
}); 
$scope.users = [] 
$scope.text='mikyas'; 
    Meetup.query({text: $scope.text}).$promise.then(function (results) { 
    $scope.users = results; 
    }, function(error) { 
    // console.log(error); 
    $scope.meetups = []; 
}); 

}]); 

任何人都可以提供代碼請。

回答

0

這是Angular的服務。以下是我在其中一個應用程序中使用的身份驗證服務示例。如果您想在關閉應用程序後讓用戶登錄,則還應該將用戶存儲在本地存儲中。

app.factory('AuthService', ['$q', '$http', 'LocalStorageService', 
    function($q, $http, LocalStorageService) { 
    var service = {}; 

    service.user = LocalStorageService.get("AUTH_USER", null); 

    service.isLoggedIn = function(){ 
     return service.user != null && service.user != undefined && service.user != ""; 
    } 

    service.checkLogged = function(){ 
     return $http.get(APPCONFIG.apiAccessPoint + "/user/" + service.user._id + "/isLogged").then(function(response){ 
      if(!response.data.success || !response.data.logged){ 
       service.logout(); 
       return false; 
      } 
      else{ 
       return true; 
      } 
     }, function(response){ 
      service.logout(); 
      return false; 
     }); 
    } 

    service.login = function(name, password){ 
     return $http.post(APPCONFIG.apiAccessPoint + "/user/login", {name: name, password: password}).then(function (response){ 
      if(response.data.success){ 
       LocalStorageService.set('AUTH_USER', response.data.data); 
       $http.defaults.headers.common.Authorization = 'Bearer ' + response.data.data.token; 
       service.user = response.data.data; 
      } 
      return response.data; 
     }, function (response){ 
      if(response.status == 400 || response.data.error_code == "VAL_ERROR"){ 
       return response.data; 
      } 
      else{ 
       return $q.reject(); 
      } 
     }); 
    } 

    service.logout = function(){ 
     // remove token from local storage and clear http auth header 
     LocalStorageService.deleteValue("AUTH_USER"); 
     $http.defaults.headers.common.Authorization = ''; 
     service.user = null; 
    } 

    return service; 
}]); 

這是你將如何使用該服務在控制器(例如顯示配置文件):

app.controller('ProfileViewCtrl', ['$scope', '$routeParams', 'AuthService', 'UserService', 
function($scope, $routeParams, AuthService, UserService) { 

    $scope.isLogged = AuthService.isLoggedIn(); 
    $scope.user = null; 
    $scope.notFound = false; 
    $scope.ownProfile = false; 

    $scope.user = UserService.getUser($routeParams.user).then(function(response){ 
     if(response.success){ 
      $scope.user = response.data; 
      $scope.notFound = response.data == undefined; 
      if(!$scope.notFound && $scope.isLogged){ 
       $scope.ownProfile = $scope.user._id == AuthService.user._id; 
      } 
     } 
     else{ 
      console.log(response.data); 
     } 
    }); 


}]); 

或者登錄頁面:

app.controller('LoginCtrl', ['$scope', '$route', 'AuthService', 
    function($scope, $route, AuthService) { 

    $scope.user = {}; 

    $scope.login = function(){ 
     AuthService.login($scope.user.name, $scope.user.password).then(function(response){ 
      if(response.success){ 
       $route.reload(); 
      } 
      else{ 
       console.log("Wrong User or password..."); 
      } 
     }); 
    } 
}]);  
+0

10Q您提出的寶貴時間,但告訴我,我的角度控制器中添加您的代碼?請幫助我 –

+0

我添加了一個示例,您將如何在控制器中使用Service。 – Jodo

+0

好的謝謝反正 –