0

It seems like when I redirect from one page to my mainpage my rootscope resets?

Here is the redirection code after login:

self.authUser = function() {
                LoginService.authUser($scope.inputEmail, $scope.inputPassword, 'inf')
                        .then(function(d) {
                            $rootScope.authenticated = true;
                            $window.location.href = '/job';
                        }, function(errResponse) {
                            $scope.errorLogin = true;
                            $rootScope.authenticated = false;
                        });
            };

Here is the code for the mainpage:

    <script src="<c:url value="/resources/js/application.js"/>"></script>
    <script src="<c:url value="/resources/js/service/main_service.js"/>"></script>
    <script
        src="<c:url value="/resources/js/controller/main_controller.js"/>"></script>
<title>Insert title here</title>
</head>
<body ng-app='application'>
<div class='container' ng-controller='MainController as ctrl'>
    <span ng-show='authenticated'>lllllllllllllllloloooooo</span>
    <button class="btn btn-lg btn-primary btn-block" ng-click='ctrl.authorize()'>Sign
            in</button>
            </div>
</body>

So after a successful login I set the rootScope to true, but then when it does $window.location.href it reset the true and its a false again. Do I need to reauthenticate every page via a post response on load or something?

Edit: ngRoute

application.js

'use strict';

var App = angular.module('application', [ 'ngRoute', 'ngCookies' ]).config(
        [ '$routeProvider', function($routeProvider) {
            $routeProvider.when('/', {
                templateUrl : '/job/',
                controller : 'main_controller'
            }).when('/login', {
                templateUrl : '/job/login',
                controller : 'login_controller'
            }).otherwise({
                redirectTo : '/'
            });
        } ]);

Tried $location.path("/"); $scope.$apply();

and $window.location.assign('/job');

also

$timeout(function () {
                            $location.path("/");
                        });

$location doesn't change page & $window refreshes my rootscope

2
  • 1
    Are you using html5 or hash based routing? Which routing system are you using? angular-route? ui-router? You are currently reloading the application, which resets the state. You need to use routes correctly to prevent reloading. Commented Dec 26, 2015 at 5:38
  • @datasage I am using ngRoute Commented Dec 26, 2015 at 14:17

1 Answer 1

0

I wasn't actually using rootscope properly and nGroute was actually not being used properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.