【发布时间】:2017-04-08 23:07:30
【问题描述】:
我已经构建了一个小模式,可以根据模型递归地创建不同的子指令。我正在使用 $compile 递归地构建子指令,然后将它们附加到父指令。
指令构建本身似乎工作得很好,但由于某种原因,嵌入式表达式或ng-bind 或插值似乎不适用于嵌套指令。
这是一个sn-p:
app.directive("child", function ($compile) {
function getTemplate(depth) {
if (depth % 2 == 0) {
return "<even depth='deeper'/>"
} else {
return "<odd depth='deeper'/>"
}
}
return {
scope: {
depth: "="
},
link: function linker($scope, $element) {
if ($scope.depth == 0) {
var child = angular.element("<span ng-bind='depth'/>");
child = $compile(child)($scope);
$element.append(child);
} else {
$scope.deeper = $scope.depth - 1;
var child = angular.element(getTemplate($scope.depth));
child = $compile(child)($scope);
$element.append(child);
}
}
}
})
基本上在这个测试中,指令会递归地向下跳,直到depth 到达0,然后吐出一个<span> 元素。
预期的结果应该是一个值为0 的span 元素。但是好像没有评价。使用 <span>{{depth}}</span> 还会生成文字 html,而不是评估内容。
我正在尝试实现嵌套<even><odd><even> 指令的结果删除 周围的<child> - 指令。
这是一个完整的 jsFiddle:https://jsfiddle.net/eg1e1aLz/
生成的 DOM 应该如下所示:
<test depth="4" class="ng-isolate-scope">
<even depth="depth-1" class="ng-scope ng-isolate-scope">
<odd depth="depth-1" class="ng-scope ng-isolate-scope">
<even depth="depth-1" class="ng-scope ng-isolate-scope">
<odd depth="depth-1" class="ng-scope ng-isolate-scope"><span ng-bind="depth" class="ng-binding ng-scope">0</span></odd>
</even>
</odd>
</even>
</test>
【问题讨论】:
-
您希望
even和odd标记如何调用child指令? -
对不起,忘了提琴...
-
你期待什么类型的输出@Michael
标签: angularjs