I am calling a service method in my controller to return an object. I struggled a lot to figure out the exact implementation difference between a service and a factory.
According to what I learned service instantiates with new and returns this to the calling controller.
I am trying to accomplish the same here..calling a service function in controller and storing the returned object in controller's $scope.user variable as follows:
controller:
app.controller('LoginFormController',['$log','$scope','userAngService','userBean',function($log,$scope,userAngService,userBean){
$scope.user = {};
$scope.login = function(val){
userAngService.login(val);
$scope.user = userAngService.user; //accessing service's user variable
//$scope.user = userAngService.user;
console.log($scope.user); //empty object being logged in console
};
}]);
service:
app.service('userAngService',['$http','$log','$rootScope','$location',function($http,$log,$rootScope,$location){
this.user = {};
this.login = function(val){
console.log('from angular service');
$http({
method: 'GET',
url: 'http://localhost:7070/messenger/webapi/messages'
}).success(function(data){
user = data;
console.log(user); //successfully logging "user" in console
});
};
return this;
}]);
Above in the service the login function(val) is changing the user variable defined in the service itself.
I can make the function to return this.user but still am getting the same result.
What changes should I make here?
