1

I'm having problems with redirect in angularJS.

For example it works perfectly fine, when the user logs in. If a user is logged in (information stored in the localstorage) it redirects him automatically to my '/dashboard'.

But when the user logs out it is not redirecting correctly but goes to 'http://localhost/ngJS/index.php#' instead of anything else I tell him. What do I do wrong?

ngJS is my project-root-folder and index.php is just a container with my libraries and code-inclusions. I am running the sites on a local server XAMMP.

mainAppMdl.js

var app = angular.module('mainApp', ['ngRoute']);
/*
 * Configuration for redirecting
 * @param {type} $routeProvider
 */
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
     //WORKING
     .when('/', {resolve: {
                    "check": function ($location) {
                        if (localStorage.getItem('usr') && 
                                localStorage.getItem('usr') !== '0') {
                            $location.path('/dashboard');
                        }
                    }
                },templateUrl: 'pages/cover.html'
            })
    //WORKING LOADING THE CONTROLLER SUCCESSFULLY
    .when('/dashboard', {
                resolve: {
                    "check": function ($location) {
                        if (localStorage.getItem('usr') === '0') {
                            $location.path('/');
                        }}},
                templateUrl: 'pages/dummy.html',
                controller: 'logoutCtrl'
            })
    .when('/login', {
                templateUrl: 'pages/login/login.html',
                controller: 'loginCtrl'
            })
    .when('/logout', {redirectTo: '/'})
    .otherwise({redirectTo: '/'});
 }]);

logoutCtrl

app.controller('logoutCtrl', function ($scope, $rootScope, $location, $log) 
{

        $log.log('logoutCtrl');
        $log.log($scope);
        $scope.message = $rootScope.message;

        $scope.logout = function () {
            if (typeof (localStorage) !== 'undefined') {
                localStorage.setItem('usr', 0);
                // NOT WORKING --------------------- redirect
                $location.path('/');
                // WORKING AND PRINTING ---------------------
                console.log('redirecting!');

            }
            /* No localstorage support */
            else {
                alert("Tut uns leid. Localstorage wird nicht unterstützt");
            }
        };

    });

logout-template.php

<section class="bg-success text-info" id="horizontal-nav-pills" >
                <nav>
                    <ul class="nav nav-pills nav-justified">
                        <li role="presentation" class="active"><a href="#">Home</a></li>
                        <li role="presentation"><a href="#/new-report"><span class="glyphicon glyphicon-plus"></span> Bericht</a></li>
                        <li role="presentation"><a href="#"><span class="glyphicon glyphicon-chevron-left"></span></a></li>
                        <li role="presentation"><a href="#"><span class="glyphicon glyphicon-chevron-right"></span></a></li>
                        <li role="presentation" ><a href="#" ng-click="logout();">Logout</a></li>
                    </ul>                           
                </nav>
            </section> 
            <!--end horizontal-nav-pills-->

3 Answers 3

1

Instead of using:

<a href="#" ng-click="logout();">Logout</a>

use:

<a href="javascript:void(0);" ng-click="logout();">Logout</a>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks that did it finally! Looking over it later I thought it was something with this link but didn't know what to change. And for styling purposes I needed an <a> element.
0
  $scope.logout = function () {
        if (typeof (localStorage) !== 'undefined') {
            localStorage.setItem('usr', 0);
            // NOT WORKING --------------------- redirect
            $location.path('/');
            // WORKING AND PRINTING ---------------------
            console.log('redirecting!');

        }
        /* No localstorage support */
        else {
            alert("Tut uns leid. Localstorage wird nicht unterstützt");
        }
    };

The problem is with:

$location.path('/');

It should have been:

$location.path('/logout');

Comments

0

Since neither ngJS nor index.php is in the code you posted I suppose that the problem comes from a different file that is currently not on your radar.

I suggest to get an editor that support's searching in mutliple files (like Notepad++) and search through the project directory for the above mentioned, two terms.

1 Comment

ngJS is just my root-folder-name.** Index.php** is just an empty container used by all my templates. I added the code, but I doubt there is a problem to be found in that.

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.