Currently, I am working on developing a basic application using Angular JS. Within this project, I have two HTML files named Login.html and Dashboard.html. When I launch the Login.html file, everything operates smoothly. Upon successful login, my goal is to display the user dashboard with JSON data retrieved from the server at the time of login.
Below is an excerpt of my code (main.js):
var app = angular.module('NovaSchedular', []);
app.factory('MyService', function()
{
var savedData = {}
function set(data)
{
savedData = data;
}
function get()
{
return savedData;
}
return {
set: set,
get: get
}
});
function LoginController($scope,$http,MyService,$location)
{
$scope.login = function(str) {
console.log(".......... login called......");
var validEmail=validateEmail(email.value);
if(validEmail && password.value != ""){
$http.post('./nova/TaskManager/public/user/login?email='+email.value+'&password='+password.value).success(function(data, status)
{
console.log(data);
var result=data.response;
console.log(result);
if (result=="success")
{
$scope.userId=data.user_id;
$scope.email=data.email;
$scope.Name=data.name;
$scope.password=data.password;
$scope.Type=data.type;
console.log("........"+$scope.userId);
console.log("........"+$scope.email);
console.log("........"+$scope.Name);
console.log("........"+$scope.password);
console.log("........"+$scope.Type);
MyService.set(data);
console.log(data);
alert(data.message);
window.location.href='./Dashboard.html';
//$location.path('./Dashboard.html', data);
}
else
alert(data.message);
});
}
}
}
function DashboardController($scope,$http,MyService)
{
$scope.userInfo = MyService.get();
}
Upon successfully logging in, I can see that the server response (JSON data) is being received under the LoginController. However, my next challenge is to have this data readily available on the Dashboard page so that it can populate the dashboard with the respective user information.
I attempted achieving this by:
window.location.href='./Dashboard.html';
//$location.path('./Dashboard.html', data);
However, I encountered an issue where the data was not being passed from the LoginController to the DashboardController despite redirecting to Dashboard.html successfully. Consequently, upon inspecting the console for Dashboard.html, an empty JSON object is displayed.
It's puzzling why the data isn't being transferred. Any insights or suggestions would be highly appreciated.