Search code examples
angularjsangularjs-factory

How can I use a method I defined in the current Factory?


I am have a factory I am working wirh that makes references to the methods defined inside the object returned by the factory. I these methods are all undefined when I alert them or console.log. Is there a way to reference methods defined in the same factory? To put it another way, is there a way to achieve what I illustrate below?

angular.module('myModule').factory('myFactory', ['$window', '$q', '$rootScope', '$http', function($window, $q, $rootScope, $http) {

  return {
    method1: function () {
       return 1;
    },
    {
    method2: function () {
       return this.method1 + 1;
    }
}]);

Solution

  • i would suggest that you do it something like this.

    angular.module('myModule').factory('myFactory', function() {
      var method1 = function(){ return 1; };
      return {
        method1: method1,
        method2: function () {
           return method1 + 1;
        }
    }]);