I've got a employeeController
and a employeeFactory
in the employeeFactory
I receive an employee
like this:
function employeeFactory(authenticationFactory,requestFactory,GLOBALS) {
var factory = {};
var vm = this;
vm.employee = {};
factory.getEmployee = function(id) {
data = {"api_token": authenticationFactory.getToken()};
url = GLOBALS.url + 'show/employee/' + id;
requestFactory.post(url, data)
.then(function (response) {
return vm.employee = response.data.result.Employee;
}, function () {
$window.location.assign('/');
});
}
return factory;
}
In my controller I'm trying to receive it like this:
console.log(employeeFactory.getEmployee($routeParams.id));
But the result is null?
When I console.log
the response in my requestFactory
I receive an employee
object. What am I doing wrong?
Reason behind it is, you missed to return promise of
requestFactory.post
fromfactory.getEmployee
methodCode
But even though you do it, you will not able to get value
employee
object printed. It will print promise object return by$http.post
method/$resource
methodFor getting hold on that object you need to use
.then
function over that promise object. like belowWe can use deferred api in angularjs for better communication between caller and service.
On your controller:
By this approach we can notify caller with some messages if your response gets delayed. For more info see this link. angular promises