1

Here i'm working on a login app using AngularJS. After authentication, when I try to redirect to home page using $location, first the url will change to '/home' but suddenly the path change and shows '/undefined'. Following is my code:

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

var currentURL = location.protocol+'//'+location.hostname+':'+location.port;

app.constant("customConstants", {"value": "false","url": currentURL});

app.config(function($routeProvider) {
    $routeProvider
    .when('/', {
        templateUrl : 'usrlib/html/login.html',
        controller : 'loginController'
    })
    .when('/login', {
        templateUrl : 'usrlib/html/login.html',
        controller : 'loginController'
    })
    .when('/home', {
        templateUrl : 'usrlib/html/home.html',
        controller : 'homeController'
    })
    .otherwise({
        redirectTo: '/'
    });
});

app.controller('loginController', function($rootScope, $scope, $http, $route, $location, customConstants) {
    console.log("Inside loginController");

    $scope.authenticate = function () {
        var userdetails = {};
             userdetails["username"]=angular.element('#username').val();
             userdetails["password"]=angular.element('#password').val();
            var config_json = {
                headers : {
                    'Content-Type': 'application/json'
                }
            }
            $http.post(customConstants.url+"/login",userdetails, config_json)
                .then(function successCallback(response) {
                console.log(JSON.stringify(response.data, null, "\t"));
                var resp = response.data;

                if(resp=="success"){
                    alert("Success login")
                    $location.path('/home');

                }
                else{
                    console.log("Login Failed");
                }
             }, function errorCallback(response) {
                console.log(response.error);
            });
     };

});

app.controller('homeController', function(customConstants, $scope, $http) {
    alert("Inside homeController");

    $http.get(customConstants.url+"/home")
        .then(function successCallback(response) {
            console.log("overall_info :: "+JSON.stringify(response.data, "\t"));

     }, function errorCallback(response) {

     });
});

In browser it shows:

enter image description here

Can't find the issue, please help out.

13
  • Are you sure the path in the mapping is correct? Please can you post the structure Commented Apr 24, 2017 at 5:57
  • @CrazyMac : in "routeProvider" i define it like .when('/home', { templateUrl : 'usrlib/html/home.html', controller : 'homeController' }) and redirect using $location.path('/home'); . Do I need to set the path mapping else where? Commented Apr 24, 2017 at 6:01
  • Based on what you have posted and if this path is correct, usrlib/html/home.html, then It should load this page. Are you trying to control the application base path anywhere else ? Also what do you see currently now in the browser? Commented Apr 24, 2017 at 6:02
  • I think you are controlling the URL using customConstants. I am not sure why do you have to use this. Can you dump and tell what's is being printed for customConstants.url Commented Apr 24, 2017 at 6:13
  • If you do not want # in the URL then you need to add $locationProvider.html5Mode(true); in the config section or else refer this basic example www.w3schools.com/angular/tryit.asp?filename=try_ng_routing Commented Apr 24, 2017 at 6:18

2 Answers 2

1

You do not need.

$location.path('/home'); // Wrong

Try

$location.path('home'); // ryt
Sign up to request clarification or add additional context in comments.

2 Comments

Is this not solving the issue
@JinsPeter : No it is not solving the issue.
0

Just try to remove the $location Parameter from your function definition. because unless you invoke the function there is a chance of showing undefined,,

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.