0

I have a service which exposes some utilities methods that are executed using $http

// my service declaration
// $http gets injected

this.getName = function(){
   $http(options)
   .then(
      function(data){
         return data;
      }, 
      function(error){
         return 'some error';
      }
   );
};

this.getAge = function(){
   $http(options)
   .then(
      function(data){
         return data;
      }, 
      function(error){
         return 'some error';
      }
   );
};

In my controllers, I get this service injected in order to expose those utilities methods such as:

// my controller declaration
var service = $injector.resolve('$myService');

Then, from here I want to call the utilities methods such as:

console.log(service.getName());

or do something like

if(service.getName() == ''){

}

The problem is that when I try to do that, the method start the async HTTP call and returns empty, while only later the answer is returned. How can I address this issue?

3
  • 1
    You can return from an AJAX call - you either use promises or callbacks. Commented Oct 12, 2016 at 15:18
  • 1
    andyshora.com/promises-angularjs-explained-as-cartoon.html Commented Oct 12, 2016 at 15:19
  • I don't get the down-voting but thanks for the answer Commented Oct 12, 2016 at 15:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.