【发布时间】:2013-12-21 10:44:08
【问题描述】:
我有两个指令
app.directive('panel1', function ($compile) {
return {
restrict: "E",
transclude: 'element',
compile: function (element, attr, linker) {
return function (scope, element, attr) {
var parent = element.parent();
linker(scope, function (clone) {
parent.prepend($compile( clone.children()[0])(scope));//cause error.
// parent.prepend(clone);// This line remove the error but i want to access the children in my real app.
});
};
}
}
});
app.directive('panel', function ($compile) {
return {
restrict: "E",
replace: true,
transclude: true,
template: "<div ng-transclude ></div>",
link: function (scope, elem, attrs) {
}
}
});
这是我的观点:
<panel1>
<panel>
<input type="text" ng-model="firstName" />
</panel>
</panel1>
错误:[ngTransclude:orphan] 在模板中非法使用 ngTransclude 指令!没有找到需要嵌入的父指令。元素:<div class="ng-scope" ng-transclude="">
我知道 panel1 不是一个实用的指令。但在我的实际应用中,我也遇到了这个问题。
我在http://docs.angularjs.org/error/ngTransclude:orphan 上看到了一些解释。但是我不知道为什么会出现这个错误以及如何解决它。
编辑 我创建了一个jsfiddle 页面。提前谢谢你。
编辑
In my real app panel1 does something like this:
<panel1>
<input type="text>
<input type="text>
<!--other elements or directive-->
</panel1>
结果 =>
<div>
<div class="x"><input type="text></div>
<div class="x"><input type="text></div>
<!--other elements or directive wrapped in div -->
</div>
【问题讨论】:
-
您是否尝试向
panel指令:require: "^panel1"投放广告? -
这两个指令不依赖。但是我在面板指令中添加了 require: "^panel1" 以进行测试,并且错误不会消失。
-
用
add a div per each children.的另外1个解决方案检查我的更新答案 -
我在编辑部分添加了解决您的问题的解决方案
标签: angularjs angularjs-directive