0

I am trying to use an $http promise from a factory withing my controller. Code below works, and I can view the users variable in the view by calling {{users}}

Working Factory:

.factory('bidsCreatedSimple', ['$http', function($http) {
    // Expose public API first, use func notation
      return { 
          getUsers: getUsers,
      };

      function getUsers(){
          return $http.get('/bids.json');
      }
}])

In the controller:

$scope.users = []; //visible in the view

activate();

function activate(){
    bidsCreatedSimple.getUsers().then(function(users){
      $scope.users = users.data;
    });
} 

console.log($scope.users); //returns a blank [] even though
                           //it renders with data in the view

How can I use the $scope.users variable in my controller? I need to use it's data for some other objects in the controller.

1 Answer 1

2

your code is fine. $scope.users DOES get filled with data, but your console.log($scope.users) is executed before the $http request returns - therefore you're console.logging the empty array you declare at the top of your code.

add the console.log line in the callback and you'll see your users (assuming data is returning correctly from your backend)

bidsCreatedSimple.getUsers().then(function(users){
      $scope.users = users.data;
      // here $scope.users is filled with the .json data
      console.log($scope.users)
      // put here your code to manipulate users (or call a function that does that properly)
    });
// here the $scope.users is undefined, because when this line executes, the request from get users hasn't returned yet (and the callback function hasn't executed either)
console.log($scope.users)

This is due to asynchrony in the ajax request - you need to wait for data until the request returns in order to use it.

Sign up to request clarification or add additional context in comments.

10 Comments

Right, but I have a bunch of objects in the controller that use $scope.users. For example $scope.gridOptions.data needs to be set to $scope.users in order for a library to render a grid. How would I set those variables to the users data?
are you using angular-ui ? I haven't used it, but you probably can set the reference to $scope.users, and then use some refresh method or something similar. Like stackoverflow.com/questions/26634063/…
But, how would I actually use that data in the controller? I need to pass it to some services as well, that will manipulate it
just call your services using yourService($scope.users). But you need to be sure your request has returned, if not you will just pass an empty array. See my code where it says "put here code to manipulate users". If that's not what you're asking, then I don't understand your question. You might want to add some code about what you're trying to achieve.
see my update. You might want to read something about asynchrony as well medium.freecodecamp.com/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.