我有角度js的问题, 我创建了login.html& home.html的.. 成功登录后,我想将页面更改为home.html。
我的路由无效,默认网址为localhost / angular< - 此显示为login.html 登录后,网址将更改为localhost / home,但不会显示home.html
我尝试路由“realpath”,这是localhost / angular / view / home.html但仍然没有好处
我的文件夹&档案订单
我的login.html代码
<html ng-app="myApp" >
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="angular-1.0.6/angular.js"></script>
<script src="app/js/app.js"></script>
</head>
<title>Desainku</title>
<body>
<h2>Login</h2>
<div ng-controller="ajaxCtrl" >
<p>
<label>Username:</label>
<input type="text" name="username" ng-model="username" />
</p>
<p>
<label>Password:</label>
<input type="password" name="password" ng-model="password" />
</p>
<input type="submit" value="submit" ng-click="submitPost()" />
<p>{{result}}</p>
</div>
</body></html>
我的app.js代码(路由和发送服务器)
var app = angular.module('myApp', []);
app.config(['$routeProvider','$locationProvider',function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when('/angular/home', {
templateUrl: 'view/home.html',
controller : 'homeCtrl'
});
}]);
function homeCtrl($scope){
alert('someone call me');
}
app.controller('ajaxCtrl', function($scope, $location) {
var url = "http://localhost:7788/login";
$scope.submitPost = function() {
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: {username: $scope.username, password: $scope.password},
success: function(data) {
$scope.$apply(function(){
$scope.result=data.message;
if (data.status === 'true') {
alert($location.path());
$location.path('home').replace();
}
});
},
error: function(data) {
alert('Error get data from server');
}
});
};
});'
我的server.js&amp;的代码user_auth.js
------------server
var express = require ('express'),
app = new express(),
calldb = require ("./user_auth.js"),
fs = require('fs');
app.post('/login',calldb.login);
------------user_auth
exports.login = function(req, res) {
connDB.check_user(req.body, function(err) {
console.log(req.header);
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
if (err) {
res.send(JSON.stringify({status: 'false', message: 'Error login'}));
} else {
res.send(JSON.stringify({status: 'true', message: 'Succesfull login'}));
}
});
};
答案 0 :(得分:4)
User2401192,
首先,您需要在html中声明ng-view:
<div ng-view></div>
现在,当您的templateUrl发生更改时(例如,您执行$location.path('home').replace();
),
$routeProvider.when('/angular/home', {
templateUrl: 'view/home.html',
controller : 'homeCtrl'
});
您的ng-view
将替换为新网址的内容。在这种情况下view/home.html
。
我想补充两件事:
答案 1 :(得分:0)
替换app.js代码
var app = angular.module('myApp', []);
app.config(['$routeProvider','$locationProvider',function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when('/angular/home', {
templateUrl: 'view/home.html',
controller : 'homeCtrl'
});
}]);
function homeCtrl($scope){
alert('someone call me');
}
app.controller('ajaxCtrl', function($scope, $window) {
var url = "http://localhost:7788/login";
$scope.submitPost = function() {
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: {username: $scope.username, password: $scope.password},
success: function(data) {
$scope.$apply(function(){
$scope.result=data.message;
if (data.status === 'true') {
alert($location.path());
//$location.path('home').replace();
return $window.location.href = 'home.html';
}
});
},
error: function(data) {
alert('Error get data from server');
}
});
};
});
我会尝试它的工作。