Reputation: 1370
I am facing problem while building a single page app with angularjs. My app has a login page. If authentication is success, app is routed to home page in which some data is displayed from $scope. However, no data is being displayed after login.
I am using location.path to route to home when sign in successful. For reference, I am trying to print "$scope.homePageDetails" in home.html but nothing is getting printed. Can someone say, what wrong am I doing and how can I resolve this issue?
index.html file:
<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 file:
<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>
angular module and controller:
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);
});
}
}]);
module:
var app = angular.module('myApp', ['ngRoute']);
router:
app.config(function ($routeProvider, $locationProvider, $httpProvider) {
$routeProvider.when('/home',
{
templateUrl: 'home.html',
controller: 'myCtrl'
});
}
Upvotes: 1
Views: 83
Reputation: 2281
You can try this
$routeProvider
.when('/home',
{
templateUrl: 'home.html',
controller: 'myCtrl'
})
.when('/login',
{
templateUrl:'login.html',
controller:'myCtrl'
})
Upvotes: 1