我在使用angularjs构建单页应用时遇到问题。我的应用程序有一个登录页面。如果身份验证成功,则应用程序将路由到主页,其中从$ scope显示一些数据。但是,登录后没有数据显示。
我在登录成功时使用location.path路由到家。作为参考,我正在尝试打印" $ scope.homePageDetails"在home.html但没有任何东西被打印。有人可以说,我做错了什么,我该如何解决这个问题?
index.html文件:
<html>
<body>
<nav>
<ul>
<li ng-hide="isUserLoggedIn"><a href="#login"> <span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</nav>
<div ng-view></div>
</body>
</html>
login.html文件:
<div>
<form class="form-signin">
<input type="text" class="form-control" placeholder="Email" ng-model="userDetails.userName" required autofocus>
<input type="password" class="form-control" placeholder="Password" ng-model="userDetails.Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit" ng-click="signIn()">
Sign in</button>
</form>
home.html的:
<p> {{homePageDetails}}</p>
角度模块和控制器:
app.controller('myCtrl', ['$scope','$http', '$location' function($scope,$uibModal,$http, $location, $window) {
$scope.signIn = function(){
$http({
url: "http://localhost:3050/postSignIn",
method: "POST",
headers: {'Content-Type': 'application/json; charset=utf-8'
},
data: $scope.userDetails
})
.then(function(response){
console.log('resp is',response);
$scope.isUserLoggedIn = true;
$location.path('/home');
$http({
url: "http://localhost:3050/getHomePageDetails",
method: "GET",
headers: {'Content-Type': 'application/json; charset=utf-8'
}
})
.then(function(response){
$scope.homePageDetails = response.data.slice(0);
}, function(response){
// failure callback
console.log('failure in getting homePageDetails',response);
});
},
function(response){
// failure callback
console.log('failure is',response);
});
}
}]);
模块:
var app = angular.module('myApp', ['ngRoute']);
路由器:
app.config(function ($routeProvider, $locationProvider, $httpProvider) {
$routeProvider.when('/home',
{
templateUrl: 'home.html',
controller: 'myCtrl'
});
}
答案 0 :(得分:1)
你可以试试这个
$routeProvider
.when('/home',
{
templateUrl: 'home.html',
controller: 'myCtrl'
})
.when('/login',
{
templateUrl:'login.html',
controller:'myCtrl'
})