这里我正在使用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) {
});
});
在浏览器中它显示:
找不到问题,请大家帮帮忙。
你肯定在映射的路径是正确的?你可以发布结构 – CrazyMac
@CrazyMac:在“routeProvider”中,我将它定义为.when('/ home',{ templateUrl:'usrlib/html/home.html', controller:'homeController' })和重定向使用$ location.path('/ home'); 。我需要设置路径映射吗? –
根据你发布的内容,如果这个路径是正确的,usrlib/html/home.html,那么它应该加载这个页面。你是否想要在其他地方控制应用程序的基本路径?你现在在浏览器中看到了什么? – CrazyMac