Returning data from the factory to the controller

Question

I'm new to AngularJS and this is my first question in stackoverflow. I've been reading a lot of possible answers for this problem but I didn't find a solution. I have this in my factory (I think the important part is between the dots):

;(() => {
app.factory('Users', users)
users.inject = ['$state', 'UserServices']

function users($state, UserServices) {
  users.users = [];
.....
  users.activeDeactive = (userId, activate) => {
     UserServices.activeDeactive(userId, activate).then(
        response => {
           console.log(response); //it shows "{status: 0}", which is the desired value
           return response;
        }
     )
  }

.....
  return users;
}
})()

And this in my controller:

;(() => {
app.controller('UsersCtrl', usersCtrl);

function usersCtrl($scope, $state, Users, users, vendors, profiles, clients) {
   Users.users = users;
.....
   $scope.activeDeactive = function(userId, activate) {
      var response = Users.activeDeactive(userId, activate);
      console.log(response); //it shows undefined
   }
.....
   };
})();

How can I also get "{status: 0}" in the controller? I'm stuck on this for a long time... I've read about waiting for a service to resolve the data, to wait for the promise, etc... but I think this problem is much more simpler, because I've already managed to send the service info to this factory... The problem now is to send the data from the factory to the controller... Thank you very much!


Show source
| angularjs   | controller   | factory   2017-11-11 23:11 2 Answers

Answers to Returning data from the factory to the controller ( 2 )

  1. 2017-11-11 23:11

    Use below syntax in controller. The reason your code not working is you have written a service called Users.activeDeactive, you are calling it from the controller but not waiting for the response. Its very important to wait for the response. If the service call is successful, it will execute then part. Here you need to catch the response like in given syntax only. In case of errors, it will go to the error part.

        ;(() => {
        app.controller('UsersCtrl', usersCtrl);
    
        function usersCtrl($scope, $state, Users, users, vendors, profiles, clients) {
           Users.users = users;
        .....
           $scope.activeDeactive = function(userId, activate) {
              Users.activeDeactive(userId, activate).then(function(data){
          response = data;
    console.log(response);
       },
       function(error){
           console.log(error);
       });
    
        .....
    
       };
    })();
    
  2. 2017-11-12 07:11

    in users factory method return promise.

    users.activeDeactive = (userId, activate) => {
     return UserServices.activeDeactive(userId, activate).then(
         response => {
            console.log(response); 
           return response;
        }
     )
    }
    

    and in controller method change to accept resolve object.

     $scope.activeDeactive = function(userId, activate) {
       Users.activeDeactive(userId, activate).then(response =>{
      console.log(response);
     });
    }
    

    Reason behind doing is .. UserService looks aync method as you have then in user factory. so controller method will call user factory method and will not wait until userservice returns data as that goes in to event loop. to make it work return promise from user factory method.

Leave a reply to - Returning data from the factory to the controller

◀ Go back