admin管理员组文章数量:1432446
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, trythis.getTaxonomy
. Or define the object first, then useobj.getTaxonomy
andreturn obj
at the end. Or give your function a name to use for recursion. – elclanrs Commented Jun 4, 2014 at 23:06
1 Answer
Reset to default 7You 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;
}]);
本文标签: javascriptAngular invoke a factory function within the same factoryStack Overflow
版权声明:本文标题:javascript - Angular invoke a factory function within the same factory - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745580318a2664583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论