When working in my controller, I encountered a situation where I needed to call a factory function recursively. Previously, the code worked fine as simple JavaScript functions not defined within the factory itself, but I wanted to improve isolation.
Here is an excerpt of the code from 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 example is somewhat simplified, but essentially, the getTaxonomy function will recursively call itself when encountering children nodes. While handling asynchronous processing and promises, everything works smoothly when this code is placed outside the factory.
The challenge I'm facing now is figuring out how to properly invoke getTaxonomy inside getTaxonomy!