如何在下面的情况下使用转译。目的是在html(部分)文件中使用标记,而不是在指令模板中定义它。
我在这里找到了一个很棒的树形指令。(来源:source) 原始链接:http://jsfiddle.net/n8dPm/ 我尝试使用转换内容来代替在指令中定义模板。我还将Angular更新到1.2.0.rc2。 更新后的链接:http://jsfiddle.net/aZx7B/2/ 出现以下错误:
TypeError: Object [object Object] has no method '$transclude'
代码:
我在这里找到了一个很棒的树形指令。(来源:source) 原始链接:http://jsfiddle.net/n8dPm/ 我尝试使用转换内容来代替在指令中定义模板。我还将Angular更新到1.2.0.rc2。 更新后的链接:http://jsfiddle.net/aZx7B/2/ 出现以下错误:
TypeError: Object [object Object] has no method '$transclude'
代码:
module.directive("tree", function($compile) {
return {
restrict: "E",
transclude: true,
scope: {family: '='},
template:
'<ul>' +
'<li ng-transclude></li>' +
'<li ng-repeat="child in family.children">' +
'<tree family="child"></tree>' +
'</li>' +
'</ul>',
compile: function(tElement, tAttr) {
var contents = tElement.contents().remove();
var compiledContents;
return function(scope, iElement, iAttr) {
if(!compiledContents) {
compiledContents = $compile(contents);
}
compiledContents(scope, function(clone, scope) {
iElement.append(clone);
});
};
}
};
});
<div ng-app="myapp">
<div ng-controller="TreeCtrl">
<tree family="family">
<p>{{ family.name }}</p>
</tree>
</div>
</div>
编辑:
在David的建议下,做了一些更改。 http://jsfiddle.net/aZx7B/3/
现在它输出“Parent”。但是更改family
为treeFamily
并没有起作用。