登录后显示连接的用户

时间:2015-04-22 16:17:42

标签: angularjs

请您帮我更新我的控制器,以便在用户登录时更改用户名。 我面临的问题是,即使在更新用户之后,字段用户名也始终为空。 你能帮我解决一下吗? 这是我的控制器

controller(
            "HomeController",
            function HomeController($scope, sessionService, accountService) {
                var loggedUSer = {};
                loggedUSer = accountService.getuser();
                if (angular.isDefined(loggedUSer))
                    $scope.username = loggedUSer.username;
                $scope.isLoggedIn = sessionService.isLoggedIn;
                $scope.logout = sessionService.logout;
            })

并在调用控制器后显示用户名

<b>{{username}}</b>

当我加载我的网页时,用户名字段为空,即使用户登录后填充了用户名,也始终显示为空。我需要更改它以显示已连接的用户。

下面是用于检索用户和登录的工厂:

factory(
            'sessionService',
            function($http) {
                var session = {};
                session.login = function(data) {
                    return $http
                            .post(
                                    "/myapp/login",
                                    "username=" + data.username
                                            + "&password=" + data.password,
                                    {
                                        headers : {
                                            'Content-Type' : 'application/x-www-form-urlencoded'
                                        }
                                    })
                            .then(
                                    session.LoggIn = function(data) {
                                        alert("login successful");
                                        localStorage.setItem("session", {});
                                        console
                                                .log(
                                                        "localStorage after login: ",
                                                        localStorage);
                                        console.log(
                                                "session after login: ",
                                                session);
                                        console.log("data after login: ",
                                                data);
                                    }, function(data) {
                                        alert("error logging in");
                                    });
                };
                session.logout = function() {
                    localStorage.removeItem("session");
                };
                session.isLoggedIn = function() {
                    return localStorage.getItem("session") !== null;
                };
                return session;
            })

factory(
            'accountService',
            function($resource, sessionService) {
                var service = {};
                service.register = function(account, profile, success,
                        failure) {
                    if (profile.id == 1) {
                        var Account = $resource("/myapp/rest/account");
                        Account.save({}, account, success, failure);
                    } else {
                        var Account = $resource("/myapp/rest/account/teacher");
                        Account.save({}, account, success, failure);
                    }
                };
                service.userExists = function(account, success, failure) {
                    var Account = $resource("/myapp/rest/account");
                    var data = Account.get({
                        username : account.username,
                        password : account.password
                    }, function() {
                        console.log("user details : ", data);
                        var accounts = data.username;
                        if (accounts && accounts.length !== 0) {
                            console
                                    .log("profile id :",
                                            data.userProfile.id);
                            service.data = data;                            
                            success(account);
                        } else {
                            failure();
                        }
                    }, failure);
                };
                service.getuser = function() {
                    if (service.data){
                        console.log (service.data);
                        return service.data;
                    }
                    else {
                        sessionService.logout();
                    }
                };
                return service;
            }).controller(
            "LoginController",
            function($scope, sessionService, accountService, $state) {
                $scope.login = function() {
                    accountService.userExists($scope.account, function(
                            account) {
                        sessionService.login($scope.account).then(
                                function() {
                                    $state.go("home");
                                });
                    }, function() {
                        alert("Error logging in user");
                    });
                };
            })

1 个答案:

答案 0 :(得分:0)

我通过添加ng-show来修复它。 这样,如果他已登录,您可以显示用户名,如果他没有登录则可以“登录”。

<div ng-show="username!==undefined">
{{username}}
</div>
<div ng-show="username===undefined">
<a href="login.html">Log in</a>
</div>

这只是一种解决方法。