웹 앱에서 데이터를 트리 구조로 표시하려고합니다. 이 작업에 Angular를 사용하고 싶었습니다.
ng-repeat를 사용하면 노드 목록을 반복 할 수 있지만 주어진 노드의 깊이가 증가하면 어떻게 중첩 할 수 있습니까?
다음 코드를 시도했지만 HTML의 자동 이스케이프로 인해 작동하지 않습니다. 또한 최종 ul 태그가 잘못된 위치에 있습니다.
나는이 문제에 대해 완전히 잘못된 길로 가고 있다고 확신합니다.
어떤 아이디어?
답변
이 바이올린을보세요
원본 : http://jsfiddle.net/brendanowen/uXbn6/8/
업데이트 : http://jsfiddle.net/animaxf/uXbn6/4779/
이것은 tree like structure
각도를 사용하여 표시하는 방법에 대한 좋은 아이디어를 제공합니다 . HTML에서 재귀를 사용하는 것입니다!
답변
부트 스트랩 CSS를 사용하는 경우 …
Bootstrap “nav”목록을 기반으로 AngularJS에 대한 간단한 재사용 가능한 트리 컨트롤 (지시적)을 만들었습니다. 들여 쓰기, 아이콘 및 애니메이션을 추가했습니다. HTML 속성이 구성에 사용됩니다.
재귀를 사용하지 않습니다.
나는 그것을 angular-bootstrap-nav-tree 라고 불렀다 (캐치 이름, 당신은 생각하지 않습니까?)
답변
이와 같은 것을 만들 때 가장 좋은 해결책은 재귀 지시문입니다. 그러나 이러한 지시문을 만들면 AngularJS가 무한 루프에 빠진다는 것을 알 수 있습니다.
이를위한 해결책은 지시문이 컴파일 이벤트 중에 요소를 제거하고 링크 이벤트에서 수동으로 컴파일하고 추가하게하는 것입니다.
나는 이 글 에서 이것 에 대해 알아 내고이 기능 을 서비스로 추상화했다 .
module.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
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);
}
}
};
}
};
}]);
이 서비스를 사용하면 트리 지시어 (또는 다른 재귀 지시어)를 쉽게 만들 수 있습니다. 다음은 트리 지시문의 예입니다.
module.directive("tree", function(RecursionHelper) {
return {
restrict: "E",
scope: {family: '='},
template:
'<p>{{ family.name }}</p>'+
'<ul>' +
'<li ng-repeat="child in family.children">' +
'<tree family="child"></tree>' +
'</li>' +
'</ul>',
compile: function(element) {
return RecursionHelper.compile(element);
}
};
});
데모는 이 Plunker 를 참조하십시오 . 나는이 솔루션을 가장 좋아합니다.
- HTML을 덜 깨끗하게 만드는 특별한 지시문이 필요하지 않습니다.
- 재귀 논리는 RecursionHelper 서비스로 추상화되므로 지시문을 깨끗하게 유지하십시오.
업데이트 : 사용자 지정 연결 기능에 대한 지원이 추가되었습니다.
답변
angular-ui-tree 는 나를 위해 좋은 일을하는 것 같습니다
답변
다음은 재귀 지시문을 사용하는 예입니다. http://jsfiddle.net/n8dPm/ https://groups.google.com/forum/#!topic/angular/vswXTes_FtM
에서 가져옴
module.directive("tree", function($compile) {
return {
restrict: "E",
scope: {family: '='},
template:
'<p>{{ family.name }}</p>'+
'<ul>' +
'<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);
});
};
}
};
});
