I have created a factory named SharedService as shown below:
angular.module('Fms').factory('SharedService', function()
{
var userdetails=false;
return userdetails;
})
Below controllers utilize this factory:
angular.module('Fms').controller('mainCtrl',['$scope','SharedService',function($scope,SharedService)
{
$scope.userdetails=SharedService;
$scope.$watch(SharedService, function(newValue, oldValue, scope)
{
console.log(SharedService)
if (newValue && newValue !== oldValue)
{
$scope.userdetails = SharedService;
}
});
}]);
angular.module("Fms").controller('LoginCtrl',['$scope','$http','$location','SharedService',function($scope,$http,$location,SharedService)
{
$scope.results ="";
$scope.name ="";
$scope.pass ="";
$scope.errormessage="";
$scope.login=function()
{
$http(
{
method: 'get',
url: '/login',
params : {username:$scope.name,password:$scope.pass},
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
}).
success(function(response)
{
if(response=="success")
{
$scope.errormessage="";
SharedService.userdetails=true;
$location.path("/home")
}
else
{
$scope.errormessage="Enter valid Username & Password";
SharedService.userdetails=false;
}
}).
error(function(response)
{
});
}
}]);
The issue is that I need to update the `userdetails` variable in the SharedService factory from the LoginCtrl controller based on authentication response. Also, the `$scope.userdetails=SharedService;` in mainCtrl needs to be updated using $watch, but it's not working as expected.