最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular invoke a factory function within the same factory - Stack Overflow

programmeradmin1浏览0评论

In my controller, I invoke a factory function that needs to call itself recursively. The code worked when they were just simple javascript functions not defined within the factory, but I am trying to isolate them a bit better.

This is what a snippet of code looks like in the controller:

myApp.controller('VisionCtrl', ['$scope', 'AppFactory', function ($scope, AppFactory,) {
    var promiseTaxo;
    promiseTaxo = AppFactory.getTaxonomy("vision3", "Vision Type");
}])

and in the factory module:

myApp.factory('AppFactory', ['$http', '$q', function($http, $q, TaxoFactory) {
    return {
        getTaxonomy: function(group, termSet) {
            ... lots of code ....
            if (termSet.length > 0) { getTaxonomy(group, childTermSet) }
        }
    }
}])

This is super simplified, but the idea is that in the getTaxonomy function, if I find children node, I recursively call myself. There is a whole lot more stuff going on where I handle the asynch nature of the processing and the promises, but when I put this code outside the factory, it works just fine.

I just don't know how to invoke the getTaxonomy inside the getTaxonomy!

In my controller, I invoke a factory function that needs to call itself recursively. The code worked when they were just simple javascript functions not defined within the factory, but I am trying to isolate them a bit better.

This is what a snippet of code looks like in the controller:

myApp.controller('VisionCtrl', ['$scope', 'AppFactory', function ($scope, AppFactory,) {
    var promiseTaxo;
    promiseTaxo = AppFactory.getTaxonomy("vision3", "Vision Type");
}])

and in the factory module:

myApp.factory('AppFactory', ['$http', '$q', function($http, $q, TaxoFactory) {
    return {
        getTaxonomy: function(group, termSet) {
            ... lots of code ....
            if (termSet.length > 0) { getTaxonomy(group, childTermSet) }
        }
    }
}])

This is super simplified, but the idea is that in the getTaxonomy function, if I find children node, I recursively call myself. There is a whole lot more stuff going on where I handle the asynch nature of the processing and the promises, but when I put this code outside the factory, it works just fine.

I just don't know how to invoke the getTaxonomy inside the getTaxonomy!

Share Improve this question asked Jun 4, 2014 at 23:00 pierrebopierrebo 9342 gold badges12 silver badges22 bronze badges 1
  • getTaxonomy is not defined, try this.getTaxonomy. Or define the object first, then use obj.getTaxonomy and return obj at the end. Or give your function a name to use for recursion. – elclanrs Commented Jun 4, 2014 at 23:06
Add a ment  | 

1 Answer 1

Reset to default 7

You can either call this.getTaxonomy() as @elclanrs mentioned or change up your factory a bit so you can call it from inside:

myApp.factory('AppFactory', ['$http', '$q', function($http, $q, TaxoFactory) {

    var AppFactory = {
        getTaxonomy: function(group, termSet) {
            ... lots of code ....
            if (termSet.length > 0) { AppFactory.getTaxonomy(group, childTermSet) }
        }
    }

    return AppFactory;
}]);
发布评论

评论列表(0)

  1. 暂无评论