<!DOCTYPE html>
<html ng-app="treeDemo">
<body>
<div ng-controller="TreeController">
<folder-tree current-folder="folder"></folder-tree>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
angular.module('treeDemo', [])
.controller("TreeController", function($scope) {
$scope.folder = {
name: 'techs',
children: [
{
name: 'server-side',
children: [
{
name: 'Java'
},
{
name: 'Python'
},
{
name: 'Node'
}
]
},
{
name: 'front-end',
children: [
{
name: 'jQuery'
},
{
name: 'Angular'
},
{
name: 'React'
}
]
}
]
}
})
.directive("folderTree", function() {
return {
restrict: "E",
scope: {
currentFolder: '='
},
templateUrl: 'template/tree.html'
};
});
<p>{{currentFolder.name}}</p>
<ul>
<li ng-repeat="subfolder in currentFolder.children">
<folder-tree current-folder="subfolder"></folder-tree>
</li>
</ul>
<p>{{currentFolder.name}}</p>
<ul>
<li ng-repeat="subfolder in currentFolder.children"
ng-include="'template/tree.html'"
ng-init="currentFolder = subfolder">
</li>
</ul>
/*
* An Angular service which helps with creating recursive directives.
* @author Mark Lagendijk
* @license MIT
*/
angular.module('RecursionHelper', []).factory('RecursionHelper', ['$compile', function($compile){
return {
/**
* Manually compiles the element, fixing the recursion loop.
* @param element
* @param [link] A post-link function, or an object with function(s) registered via pre and post properties.
* @returns An object containing the linking functions.
*/
compile: function(element, link){
// Normalize the link parameter
// 如果link参数是对象类型link:{pre: function(...){}, post: function(...){}}则不做处理
// 如果link参数是函数类型则将其作为post-link函数在$compile之后调用
if(angular.isFunction(link)){
link = { post: link };
}
// Break the recursion loop by removing the contents
// 获取并清空当前元素的内容,后面进行编译
var contents = element.contents().remove();
var compiledContents;
return {
pre: (link && link.pre) ? link.pre : null,
/**
* Compiles and re-adds the contents
* 编译和重新添加内容到当前元素
*/
post: function(scope, element){
// Compile the contents
if(!compiledContents){
compiledContents = $compile(contents);
}
// Re-add the compiled contents to the element
compiledContents(scope, function(clone){
element.append(clone);
});
// Call the post-linking function, if any
if(link && link.post){
link.post.apply(null, arguments);
}
}
};
}
};
}]);
angular.module('treeDemo', ['RecursionHelper'])
.controller("TreeController", function($scope) {
$scope.folder = {
name: 'techs',
children: [
{
name: 'server-side',
children: [
{
name: 'Java'
},
{
name: 'Python'
},
{
name: 'Node'
}
]
},
{
name: 'front-end',
children: [
{
name: 'jQuery'
},
{
name: 'Angular'
},
{
name: 'React'
}
]
}
]
}
})
.directive("folderTree", function(RecursionHelper) {
return {
restrict: "E",
scope: {
currentFolder: '='
},
templateUrl: 'template/tree.html',
compile: function(element) {
// 我们这里使用RecursionHelper的compile方法编译指令当前元素,这里第二个参数指定一个函数,相当于常用的link函数
// 当然我们也可以指定一个对象,里面包含pre和post函数,分别对应pre-link和post-link
return RecursionHelper.compile(element, function(scope, iElement, iAttrs, controller, transcludeFn){
// Define your normal link function here.
// Alternative: instead of passing a function,
// you can also pass an object with
// a 'pre'- and 'post'-link function.
// 这里可以往scope中绑定一些变量
scope.variable = 'hello world';
});
}
};
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有