2

I am trying to develop a web application using AngularJS. I am not able to route the web pages using ngRoute. I have written web server in Bottle python framework, which throws no error on this part. The browser console shows this straneg error which I am not able to debug saying "RangeError: Maximum Call Stack Size Exceeded". Following are the code snippets and console stack trace.

app.js

var app = angular.module('tslApp', ['ngRoute','datatables','ngCookies']);

app.constant('AUTH_EVENTS', {
      loginSuccess: 'auth-login-success',
      loginFailed: 'auth-login-failed',
      logoutSuccess: 'auth-logout-success',
      sessionTimeout: 'auth-session-timeout',
      notAuthenticated: 'auth-not-authenticated',
      notAuthorized: 'auth-not-authorized'
})

app.run(function ($rootScope,$location,AUTH_EVENTS,$cookieStore,$http,$q,$timeout) {
    $rootScope.flag = false;
    $rootScope.$on('$routeChangeStart', function(evt, absNewUrl, absOldUrl) {
        $cookieStore.get(AUTH_EVENTS);
        if($rootScope.AUTH_EVENTS != AUTH_EVENTS.loginSuccess && !$cookieStore.get(AUTH_EVENTS)){    
            //console.log($rootScope.AUTH_EVENTS);
            $location.path('/');
            //$route.reload();
        }
        if(absNewUrl.$$route.originalPath == '/dashboard' && $rootScope.flag == false){
            $rootScope.flag = true;
        }
            //here you can check for your own condition and if not logged in then set $location.path(loginpath);
      });
});

route.js

app.config(function($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl : '/assets/app/partials/login.html',
            controller : 'loginCtrl'
        })         
        .when('/dashboard', {
            templateUrl : '/assets/app/partials/main.html'
        })
        .otherwise({
            redirectTo: '/'
        });
});

loginCtrl.js

app.controller('loginCtrl', ['$scope','$rootScope', 'loginFactory', function($scope, $rootScope, loginFactory) {
    $(function() {
        $('#toggle-login').click(function() {
            $('#login').toggle();
        });
    })
    $scope.errormessage = new Object();
    $scope.successMessage = new Object();
    $scope.errormessage.isError = false;
    $scope.successMessage.isSuccess = false;
    $rootScope.AUTH_EVENTS = "auth-not-authorized";
    $scope.login = function(credentials) {
        console.log(credentials);
        loginFactory.login(credentials);
    }
}])

loginFactory.js

app.factory('loginFactory', ['$http', '$q','$rootScope','AUTH_EVENTS' ,'$location','$cookieStore', function ($http,$q,$rootScope,AUTH_EVENTS,$location,$cookieStore) {
    var loginData = {};

    loginData.login = function(credentials) {
        var $promise = $http.post('/login', credentials); //send data to server.py
        $promise.then(function(msg) {
            console.log(msg);
            if (msg.data == 'success') {
                $rootScope.AUTH_EVENTS = AUTH_EVENTS.loginSuccess;
                $cookieStore.put(AUTH_EVENTS, AUTH_EVENTS.loginSuccess);
                $location.path('/dashboard');
            } else {
                $rootScope.AUTH_EVENTS = AUTH_EVENTS.loginFailed;
                scope.errormessage.isError = true;
                scope.errormessage.msg = "Wrong username and password";
                console.log("failed 1");
            }
        }, function() {
            $rootScope.AUTH_EVENTS = AUTH_EVENTS.loginFailed;
            console.log("failed 2");
        });
    }
    return loginData;
}])

console.output and errors:

Object {username: "cdcd", password: "cdcdc"}
loginFactory.js:7 Object {data: "success", status: 200, config: Object, statusText: "OK"}
angular.js:11383 RangeError: Maximum call stack size exceeded
    at invokeLinkFn (http://localhost:7000/assets/js/angular.js:8127:30)
    at nodeLinkFn (http://localhost:7000/assets/js/angular.js:7637:11)
    at compositeLinkFn (http://localhost:7000/assets/js/angular.js:6993:13)
    at compositeLinkFn (http://localhost:7000/assets/js/angular.js:6996:13)
    at compositeLinkFn (http://localhost:7000/assets/js/angular.js:6996:13)
    at compositeLinkFn (http://localhost:7000/assets/js/angular.js:6996:13)
    at publicLinkFn (http://localhost:7000/assets/js/angular.js:6872:30)
    at link (http://localhost:7000/assets/js/angular-route.min.js:7:388)
    at invokeLinkFn (http://localhost:7000/assets/js/angular.js:8125:9)
    at nodeLinkFn (http://localhost:7000/assets/js/angular.js:7637:11) <div ng-view="" class="ng-scope">(anonymous function) @ angular.js:11383(anonymous function) @ angular.js:8459invokeLinkFn @ angular.js:8127nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664
VM101:1122 <error>InjectedScript._isHTMLAllCollection @ VM101:1122InjectedScript.isPrimitiveValue @ VM101:285InjectedScript.RemoteObject @ VM101:1251InjectedScript._wrapObject @ VM101:387InjectedScript.wrapObject @ VM101:298(anonymous function) @ angular.js:11383(anonymous function) @ angular.js:8459invokeLinkFn @ angular.js:8127nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993publicLinkFn @ angular.js:6872boundTranscludeFn @ angular.js:7011controllersBoundTransclude @ angular.js:7664v @ angular-route.js:865link @ angular-route.js:831invokeLinkFn @ angular.js:8125nodeLinkFn @ angular.js:7637compositeLinkFn @ angular.js:6993compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996compositeLinkFn @ angular.js:6996publicLinkFn @ angular.js:6872link @ angular-route.js:915invokeLinkFn @ angular.js:8125
5VM101:1122 <error>

1 Answer 1

3

This error usually occurs when there is a route loop created at the below code

app.run(function ($rootScope,$location,AUTH_EVENTS,$cookieStore,$http,$q,$timeout) {
$rootScope.flag = false;
$rootScope.$on('$routeChangeStart', function(evt, absNewUrl, absOldUrl) {
    $cookieStore.get(AUTH_EVENTS);
    if($rootScope.AUTH_EVENTS != AUTH_EVENTS.loginSuccess && !$cookieStore.get(AUTH_EVENTS)){    
        //console.log($rootScope.AUTH_EVENTS);
        $location.path('/');
        //$route.reload();
    }
    if(absNewUrl.$$route.originalPath == '/dashboard' && $rootScope.flag == false){
        $rootScope.flag = true;
    }
        //here you can check for your own condition and if not logged in then set $location.path(loginpath);
  });
});

You might be redirecting to a route from loginfactory and due to some condition it is being processed again and again at $routeChangeStart event

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

4 Comments

I don't see such a looping happen seeing the code. In the loginFactory, based on the server message 'success', I am changing the route using $location.path to '/dashboard' which I have mentioned as a route in route.js . But it does not move to the page assigned as template with this route. Can you pin point me the error where according to you may be the loop occurence?
Are you able to land on the home page with route "/"?If yes are there any errors in console?
While being on the route '/', there are no errors on the console.
Still I dont have a clear ans to it.we'll have to debug it.Add a breakpoint at both the if blocks and check if they are excuted again and again.I'm sure it is something related with the routechangestate event only.(Just for information,rootscope is cleared when you refresh the app by yourself Make sure that you are not checking any such scenario)

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.