1

I have two function in factory

here is some code

.factory('getArticles', function ($http, $q) {
   return {
       abc : function() {
        var deferred = $q.defer();
        // my code 
            res= this. bcd();
            console.log(res); // undefined
          deferred.resolve(res);

          return deferred.promise;
       },
       bcd: function() {

        //some codee 

         return res; 

       }
   }

});

Now from controller i am calling abc function but when i checked value of res under logs. this is showing undefined.

Any idea ? how to do this ?

Thanks

6
  • May be you don't call abc function like getArticles.abc() and call this without correct this context? Commented May 28, 2015 at 9:13
  • i am able to call abc. Commented May 28, 2015 at 9:15
  • I tried to say that you can pass getArticles.abc function in callback and it can called without correct this context. Commented May 28, 2015 at 9:17
  • what make your bcd function ? maybe could you move it outside the .factory? look also at stackoverflow.com/questions/16227644/…. to my understanding you should have only one function which return one result in your factory. Commented May 28, 2015 at 9:17
  • @HituBansal does it throw an error on calling this.bcd() ? Commented May 28, 2015 at 9:28

3 Answers 3

3

@Vineet's answer was correct for services, which are instantiated, but factories should just return an object.

I like @peek4y's answer, but it can be further improved, to still have abc() private:

(function () {
'use strict';

var getArticles = function ($http, $q) {

  function bcd() {
    var res;
    // some logic
    return res;
  }

  function abc() {
    var deferred = $q.defer();
    var res = bcd();
    console.log(res); 
    deferred.resolve(res);
    return deferred.promise;
  }

  //return only the functions you want to expose

  return {
    abc: function () {
      return abc();
    }
  }
});

angular.module('myApp')
  .factory('getArticles', getArticles);

}());
Sign up to request clarification or add additional context in comments.

Comments

1

Always, separate the concerns.

.factory('getArticles', function ($http, $q) {

  function abc() {
    var deferred = $q.defer();
    res= bcd();
    console.log(res); 
    deferred.resolve(res);
    return deferred.promise;
  }

  function bcd() {
    //some logic
    //Make sure your logic is returning proper value.
    return res;
  }


  //return only those functions you want to expose
  return {
    abc: abc
  }
});

in the return statement, you can basically expose only those methods, which you would consume.

Comments

0

Yes exactly as Mr. luschn has said but you can call your factory/service function from the controller by changing as below defined. You should define your function with this reference in factory

.factory('getArticles', function ($http, $q) {

       this.abc = function() {
        var deferred = $q.defer();
        // my code 
            res= this. bcd();
            console.log(res); // undefined
          deferred.resolve(res);

          return deferred.promise;
       }

       this.bcd = function() {

        //some codee 

         return res; 

       }

});

in your controller you can call by

getArticles.abc()

If you need to call a factory's function from another factory function call simply

this.bcd();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.