0
在下面的代碼中給出了在工廠失敗的遞歸調用。我期待的是它應該提醒所有的名字,如 Level2-1,Level3-1,Level4-1,Level4-2等等。如果我評論遞歸調用行,那麼它會正確提醒所有level2名稱,例如Level2-1,Level2-2,Level2-3,Level2-4。請提出最新的錯誤?遞歸的angularjs工廠函數失敗
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Angular App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script type="text/javascript">
(function() {
'use strict';
function gridFactory($http) {
var service = {
treeCollapse: CollpaseFunction,
drawd3Dia: D3DiagramFunction,
callParams: {}
};
return service;
function D3DiagramFunction() {
var treeData = {
name: "Level1-1", children: [
{
name: "Level2-1",
children: [
{
name: "Level3-1",
children: [
{
name: "Level4-1",
children: []
},
{
name: "Level4-2",
children: []
}
]
},
{
name: "Level3-2",
children: []
}
]
},
{
name: "Level2-2",
children: []
},
{
name: "Level2-3",
children: []
},
{
name: "Level2-4",
children: []
}
]
}
treeData.children.forEach(this.treeCollapse);
}
function CollpaseFunction(c) {
alert(JSON.stringify(c.name));
// The follwing call seems to be failing. Why?? Please correct this.
// I intend this to be a recursive call, calling it self(CollpaseFunction).
// What am I doing wrong? What I am expecting is it should alert all the names
// Level2-1, Level3-1, Level4-1, Level4-2 and so on. But its failing.
// If I comment the following line, then it alerts all the level2 names
// correctly such as Level2-1, Level2-2, Level2-3, Level2-4
//c.children.forEach(this.treeCollapse);
// The following is also similarly not working. Using gridFactory.treeCollapse
// instead of this.treeCollapse
//c.children.forEach(gridFactory.treeCollapse);
}
}
function patientCategoryController($scope, $controller, gridFactory) {
gridFactory.drawd3Dia();
}
angular.module('abvhHisApp', []);
angular.module('abvhHisApp').factory('gridFactory', gridFactory);
gridFactory.$inject = ['$http'];
angular.module('abvhHisApp').controller('patientCategoryController', patientCategoryController);
patientCategoryController.$inject = ['$scope', '$controller', 'gridFactory'];
}())
</script>
</head>
<body>
<div data-ng-app="abvhHisApp">
<div data-ng-controller="patientCategoryController">
<h1>Dude...Happy New Year to you!!!! :)</h1>
</div>
</div>
</body>
</html>