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?

  • not clear what you are asking? – Pankaj Parkar Sep 03 '15 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 –  Sep 03 '15 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. – egon12 Sep 03 '15 at 18:25
  • Maybe http://stackoverflow.com/questions/13088153/how-to-http-synchronous-call-with-angularjs will help you. – egon12 Sep 03 '15 at 18:29
  • How would I set this up? –  Sep 03 '15 at 18:29

2 Answers2

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.

Long Nguyen
  • 426
  • 3
  • 8
  • I've seen this answer before, thank you for your consideration, but the controller doesn't have to do with my problem. –  Sep 03 '15 at 18:45
  • 1
    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. – Long Nguyen Sep 03 '15 at 18:50
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;
 }
kjonsson
  • 2,799
  • 2
  • 14
  • 23
  • 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. – Tony Sep 03 '15 at 18:29
  • I like the idea, but Angular is actually calling this code and we can't modify that –  Sep 03 '15 at 18:29