gpt4 book ai didi

javascript - Angular 递归

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:53 25 4
gpt4 key购买 nike

有两种方法可以创建 Angular 递归

第一种使用$compile函数,手动编译内容


链接:Angularjs: understanding a recursive directive

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);
});
};
},

示例:http://jsfiddle.net/n8dPm/


第二个使用 ng-include

链接:https://groups.google.com/forum/?fromgroups=#!topic/angular/TbpjE-5XEM0

<script type="text/ng-template"  id="tree_item_renderer.html">
{{data.name}}
<button ng-click="add(data)">Add node</button>
<button ng-click="delete(data)" ng-show="data.nodes.length > 0">Delete nodes</button>
<ul>
<li ng-repeat="data in data.nodes" ng-include="'tree_item_renderer.html'"></li>
</ul>

示例:http://jsfiddle.net/brendanowen/uXbn6/8/


我的问题是:两者的优缺点是什么是否可以在 ng-include 中使用包含自定义指令的模板?

最佳答案

我相信您错过了两个有值(value)的选择。

关于javascript - Angular 递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22657324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com