1
.factory('someFac', function (CONFIG, $injector, $http) {
   return $http.get('url').then(function (response) {
      var temp = response.data.answer;
      var answer = '';
      if(temp == bar){
          answer = barAnswer;
       }
      else if(temp == foo){
          answer = fooAnswer;
      }
      return $injector.get(answer);
   }
}

So my problem is, the $http.get isn't finished getting the response before the factory is finished executing...how should I fix this? I read somewhere I could implement the app.config to inject variables into a provider...could that be a solution?

5
  • not clear what you are asking? Commented Sep 3, 2015 at 18:18
  • The answer variable's value is staying at ' ' when the $injector is returned because the $http.get promise isn't executing quickly enough Commented Sep 3, 2015 at 18:21
  • why not create the object you want inside then function? $http.get is work asyncrhounously, maybe some other function can work synchrounously. Commented Sep 3, 2015 at 18:25
  • Maybe stackoverflow.com/questions/13088153/… will help you. Commented Sep 3, 2015 at 18:29
  • How would I set this up? Commented Sep 3, 2015 at 18:29

2 Answers 2

1

Your factory/service should be what is maintaining the data and the controller should just use it (this way 2-way binding can occur across different controllers).

.factory('someFac', function ($http) {
    var self = this;
    self.init = function() {
        return $http.get('url').then(function(response) {
            self.data = response.data;
        });
    }
}

Now your factory can be injected anywhere and maintains the data, so all of your controllers will be updated real time. To make sure the data is loaded make sure to put the init into a resolve in the route.

resolve: {
    someFac: function (someFac) {
        return someFac.init();
    }
}

Now in a controller you can just use it as so:

.controller('someControleler', function ($scope, someFac) {
    $scope.data = someFac.data;
});

Hope this helps.

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

2 Comments

I've seen this answer before, thank you for your consideration, but the controller doesn't have to do with my problem.
Right, my post doesn't say your controller is the issue. Your issue is you are not resolving the promise, therefore your page is loading before you get the data.
0

Usually a factory will just return the promise from calling a method with $http and then the code that called the factory will handle the ".then" when the promise is fulfilled.

.factory('someFac', function (CONFIG, $injector, $http) {
    var fact = {}

    fact.getResponse = function (){

      return $http.get('url');

    }

   return fact;
 }

2 Comments

While I absolutely agree with this, I'm not sure it solves the OP's question. But, I think it would be a step in the right direction for his refactor.
I like the idea, but Angular is actually calling this code and we can't modify that

Your Answer

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