重定向页面时丢失值

时间:2015-09-19 18:55:38

标签: javascript html angularjs

我有登录表单,当用户输入有效的用户名和密码时,他会被重定向到主页。我在会话中保留用户名和密码,但是当我转到主页并尝试从sessionservice中调用console.log geUser()函数时,我得到了null ....

您可以在下面看到我的代码。

App config

   (function () {

            'use strict';
            angular.module('chatApp', ['ui.router','ngAnimate','ui.bootstrap','ui.bootstrap.modal'])
                .config(function ($stateProvider, $urlRouterProvider) {

                    $urlRouterProvider.otherwise("/login");

                    $stateProvider
                        .state('login', {
                            url: '/login',
                            templateUrl: 'src/user/login/login.tpl.html',
                            controller: 'loginController'
                        })
                        .state('home', {
                            url: '/home',
                            templateUrl: 'src/home/home.tpl.html',
                            controller: 'homeController'

                        })
                });
        })();

登录控制器

/**
 * Created by user on 15.09.2015.
 */
(function () {
    'use strict';
    function LoginController($scope, loginService, redirectService,sessionService) {
        $scope.loginController = {};



        $scope.loginController.user = {

            username: '',
            password: ''
        };

        $scope.loginController.message = "";
        $scope.loginController.hasError = false;

        $scope.loginController.login = function () {
            var promise = loginService.login($scope.loginController.user);
            promise.then(function (data) {

                    sessionService.setUser(data);
                    redirectService.loginToHome();
                },
                function (data) {
                    $scope.loginController.message = "Username or password is incorrect";
                    $scope.loginController.hasError = true;
                });
        };

    }

    angular.module('chatApp').controller('loginController', LoginController);
})();

登录模板

<div class="modal-dialog">
    <div class="loginmodal-container">
        <h1>Login</h1><br>

        <form name="form" novalidate>
            <div class="input-group">
                <label for="user">Username </label>
                <input ng-model="loginController.user.username" id="user" name="user" type="text" class="form-control"
                       placeholder="Username = Bukic"
                       aria-describedby="basic-addon1" required/>
                <br/>

                <div ng-show="form.$submitted || form.user.$touched">
                    <div ng-show="form.user.$error.required">Username is required</div>
                </div>
            </div>
            <div class="input-group">
                <label for="pass">Password </label>
                <input ng-model="loginController.user.password" id="pass" name="pass" type="password"
                       class="form-control"
                       placeholder="Password = bukic"
                       aria-describedby="basic-addon1" required>
                <br/>

                <div ng-show="form.$submitted || form.pass.$touched">
                    <div ng-show="form.pass.$error.required">Password is required</div>
                </div>
            </div>
            <br/>
            <input type="submit" class="login loginmodal-submit" ng-click="loginController.login()"
                   ng-disabled="form.$invalid" name="login" value="Login">
        </form>
        <br/>

        <div class="alert alert-danger" ng-show="loginController.hasError">{{loginController.message}}</div>
    </div>
</div>

登录服务

/**
 * Created by user on 15.09.2015.
 */
(function () {
    'use strict';
    angular.module('chatApp').factory('loginService', function ($q) {
        var service = {};
        service.login = function (user) {
            var deferred = $q.defer();

            if (user.username === 'Bukic' && user.password === 'bukic') {
                deferred.resolve(user);
            }
            else {
                deferred.reject({success: false});
            }

            return deferred.promise;
        };
        return service;
    });
})();

会话服务

/**
 * Created by user on 15.09.2015.
 */
(function () {
    'use strict';
    angular.module('chatApp').factory('sessionService', function () {
        var service = {};
        var user = null;
        service.getUser = function () {
            console.log(user.username);
            return user;
        };
        service.setUser = function (user) {
            this.user = user;
           // console.log(user);
        };
        service.isAuthenticated = function () {
            return this.user !== null;
        };
        return service;
    });
})();

重定向服务

/**
 * Created by user on 15.09.2015.
 */
(function () {
    'use strict';
    angular.module('chatApp').factory('redirectService', function ($state) {
        var service = {};
        service.loginToHome = function () {

            $state.go('home',{reload:false});
        };

        service.loginToLogin = function(){
            $state.go('login');
        };
        return service;
    });
})();

家庭控制器

(function(){

'use strict';
function HomeController($scope,redirectService,sessionService) {

    $scope.homeController = {};

    //$scope.homeController.checkuser = sessionService.getUser();
  //  console.log("Home Controller" + $scope.homeController.checkuser);


}


angular.module('chatApp').controller('homeController', HomeController);

})();

0 个答案:

没有答案