use*_*578 23 tree json html-lists angular
我想将JSON树转换为Angular2中的无序列表.我知道Angular1的递归指令解决方案,我很确定Angular2中的解决方案也是递归的.
[
{name:"parent1", subnodes:[]},
{name:"parent2",
subnodes:[
{name:"parent2_child1", subnodes:[]}
],
{name:"parent3",
subnodes:[
{name:"parent3_child1",
subnodes:[
{name:"parent3_child1_child1", subnodes:[]}
]
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
这个无序的清单
<ul>
<li>parent1</li>
<li>parent2
<ul>
<li>parent2_child1</li>
</ul>
</li>
<li>parent3
<ul>
<li>parent3_child1
<ul>
<li>parent3_child1_child1</li>
</ul>
</li>
</ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
使用Angular2和ngFor.有人有个主意吗?
Jam*_*ord 36
您不需要创建新tree-view组件来执行此操作,只需在任何模板中使用此模式:
如果您的数据数组是list组件的公共属性:
<h1>Angular 2 Recursive List</h1>
<ul>
<ng-template #recursiveList let-list>
<li *ngFor="let item of list">
{{item.name}}
<ul *ngIf="item.children.length > 0"> <!-- item.subnodes.length -->
<ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.children }"></ng-container>
</ul>
</li>
</ng-template>
<ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: list }"></ng-container>
</ul>
Run Code Online (Sandbox Code Playgroud)
这是一个要点.
Car*_*gas 31
借用Torgeir Helgevold的帖子,我想出了这个Plunkr.这是代码:
TreeView组件:
import {Component, Input} from 'angular2/core';
@Component ({
selector: 'tree-view',
directives: [TreeView],
template: `
<ul>
<li *ngFor="#node of treeData">
{{node.name}}
<tree-view [treeData]="node.subnodes"></tree-view>
</li>
</ul>
`
})
export class TreeView {
@Input() treeData: [];
}
Run Code Online (Sandbox Code Playgroud)
应用组件:
import {Component} from 'angular2/core';
import {TreeView} from './tree-view.component';
@Component({
selector: 'my-app',
directives: [TreeView],
template: `
<h1>Tree as UL</h1>
<tree-view [treeData]="myTree"></tree-view>
`
})
export class AppComponent {
myTree = [
{name:"parent1", subnodes:[]},
{name:"parent2",
subnodes:[
{name:"parent2_child1", subnodes:[]}
],
{name:"parent3",
subnodes:[
{name:"parent3_child1",
subnodes:[
{name:"parent3_child1_child1", subnodes:[]}
]
}
]
}
];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12993 次 |
| 最近记录: |