在角度身份验证中显示当前用户名

时间:2016-02-22 13:45:50

标签: javascript angularjs angularjs-directive angular-ui-router

我正在处理已由其他人制作的登录脚本。我的目的实际上是整合使用ng-option而不是input。从示例中可以看出,这很有效。但是当我登录时,我看不到用户的数据。为什么呢?

完整代码:http://plnkr.co/edit/ImsqhVFVanCp5OXNisrA?p=preview

Controllers.js:

angular.module('Authentication')

.controller('LoginController',
['$scope', '$rootScope', '$location', 'AuthenticationService',
function ($scope, $rootScope, $location, AuthenticationService) {
    // reset login status
    AuthenticationService.ClearCredentials();
         $scope.users = [
                {"username": "test", "number": "13242342"},
                {"username": "2", "number": "00000000"},
                {"username": "3", "number": "0483184"},
            ];
    $scope.login = function () {
        $scope.dataLoading = true;
        AuthenticationService.Login($scope.username, $scope.password,        function(response) {
            if(response.success) {
                AuthenticationService.SetCredentials($scope.username,     $scope.password);
                $location.path('/');
            } else {
                $scope.error = response.message;
                $scope.dataLoading = false;
            }
        });
    };
}]);

1 个答案:

答案 0 :(得分:1)

在您的身份验证服务中,您可以像这样设置用户的凭据:

$rootScope.globals = {
    currentUser: {
        username: username,
        authdata: authdata
    }
};

因此,在您看来,您需要将{{user.username}}更改为{{globals.currentUser.username}}