我有一个树形结构。 JSBIN here这个函数为什么执行两次?
在指令中scope.add_child_task = function() {
scope.add_task(scope.path,"child of " + scope.member.name);
if (!scope.has_children) {
scope.add_children_element();
scope.has_children = true;
}
};
在控制器
$scope.add_task = function(to,name) {
DataFactory.add_task(to,name);
};
工厂找到正确的位置和加入的节点。
当添加一个孩子到有现有孩子的节点时,它添加了两个孩子,我不明白为什么。
感谢。
编辑 我可以失去has_children
,它仍然产生相同的结果
会员链接载体作用
link: function (scope, element, attrs) {
element.append("<collection></collection>");
$compile(element.contents())(scope);
scope.get_path = function() {
var temp = scope.$parent.get_path();
temp.push(scope.member.name);
return temp;
};
scope.path = scope.get_path();
scope.add_child_task = function() {
scope.add_task(scope.path,"child of " + scope.member.name);
};
}
EDIT 2 DROP掉的for循环,以及 - 只是交换引用,除了函数e,执行两次!
这是没有答案,但在 “add_children_element()”你正在调用“scope.has_children = true;”和“add_task()”。 检查你是否没有在你的DataFactory中重复这个错误,并尝试记录(console.log)代码的确切含义。 – Neozaru
我相信你想在第一个函数体内使用'this',而不是'scope' –
@Mark Reed--你能解释一下吗? '这个'而不是什么? – haki