简体   繁体   English

AngularJs从工厂获得员工

[英]AngularJs get employee from factory

I've got a employeeController and a employeeFactory in the employeeFactory I receive an employee like this: 我有一个employeeControlleremployeeFactoryemployeeFactory我收到一个employee是这样的:

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. 当我console.log在我的requestFactory的响应时,我收到一个employee对象。 What am I doing wrong? 我究竟做错了什么?

Reason behind it is, you missed to return promise of requestFactory.post from factory.getEmployee method 其背后的原因是,您错过了从factory.getEmployee方法返回requestFactory.post承诺factory.getEmployee

Code

factory.getEmployee = function(id) {
  data = {"api_token": authenticationFactory.getToken()};
  url = GLOBALS.url + 'show/employee/' + id;
  return requestFactory.post(url, data)
    .then(function (response) {
      return vm.employee = response.data.result.Employee;
  }, function () {
      $window.location.assign('/');
  });
}

But even though you do it, you will not able to get value employee object printed. 但是,即使您这样做,也将无法获得价值employee对象的打印。 It will print promise object return by $http.post method/ $resource method 它将通过$http.post方法/ $resource方法打印promise对象返回

For getting hold on that object you need to use .then function over that promise object. 为了控制该对象,您需要在该promise对象上使用.then函数。 like below 像下面

employeeFactory.getEmployee($routeParams.id).then(function(employee){
   console.log('Employee', employee)
})

We can use deferred api in angularjs for better communication between caller and service. 我们可以在angularjs中使用延迟的api,以在调用方和服务之间实现更好的通信。

factory.getEmployee = function(id) {
 var deferred = $q.defer();
  data = {"api_token": authenticationFactory.getToken()};
  url = GLOBALS.url + 'show/employee/' + id;
  return requestFactory.post(url, data)
    .then(function (response) {     
          deferred.resolve(response.data.result.Employee);
  }, function () {
      deferred.reject();
  });
  return deferred.promise;
}

On your controller: 在您的控制器上:

employeeFactory.getEmployee($routeParams.id).then(function(employee){
   console.log('Employee', employee)
},function(){
 $window.location.assign('/');
})

By this approach we can notify caller with some messages if your response gets delayed. 通过这种方法,如果您的响应被延迟,我们可以通知呼叫者一些消息。 For more info see this link. 有关更多信息,请参见此链接。 angular promises 有角度的承诺

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM