2017-04-24 172 views
1

这里我正在使用AngularJS的登录应用程序。身份验证后,当我尝试使用$ location重定向到主页时,首先将URL更改为“/ home”,但突然路径更改并显示“/ undefined”。以下是我的代码:

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) { 

    }); 
}); 

在浏览器中它显示:

enter image description here

找不到问题,请大家帮帮忙。

+0

你肯定在映射的路径是正确的?你可以发布结构 – CrazyMac

+0

@CrazyMac:在“routeProvider”中,我将它定义为.when('/ home',{ templateUrl:'usrlib/html/home.html', controller:'homeController' })和重定向使用$ location.path('/ home'); 。我需要设置路径映射吗? –

+0

根据你发布的内容,如果这个路径是正确的,usrlib/html/home.html,那么它应该加载这个页面。你是否想要在其他地方控制应用程序的基本路径?你现在在浏览器中看到了什么? – CrazyMac

回答

0

试着从你的函数定义中删除$ location参数。因为除非您调用该功能,否则有机会显示未定义的字词,因此您不需要。

+0

不,它仍然没有工作 –

1

您不需要。

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

尝试

$location.path('home'); // ryt 
+0

我认为它会工作(Y) –

+0

不,它不工作 –

+0

这是不是解决问题 –