1
我在使用angularjs構建單頁面應用程序時遇到了問題。我的應用程序有一個登錄頁面。如果身份驗證成功,則應用程序將路由到主頁,其中顯示$ scope的一些數據。但是,登錄後不會顯示任何數據。
我正在使用location.path路由到家時登錄成功。作爲參考,我試圖在home.html中打印「$ scope.homePageDetails」,但沒有打印出來。有人可以說,我在做什麼錯,我該如何解決這個問題?
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'
});
}
什麼是登錄頁面的控制器?您能否提供完整的路由器代碼 – Akashii
您的主屏幕範圍可能與登錄屏幕中的範圍不同。 –
@Thanh:我已經更新了控制器。沒有多個控制器。我只使用一個控制器。即myCtrl。 – Bobby