3 回答
TA贡献1871条经验 获得超13个赞
您可以使用递归组件。
首先,您可以定义一个文件夹接口:
interface Folder {
id: number;
folderName: string;
files: string[];
folders: Folder[];
}
然后用它来正确定义您的数据:
const folder: Folder = {
id: 0,
folderName: "Folder0",
files: ["File 1", "File 2"],
folders: [
{
id: 1,
folderName: "Folder1",
files: ["File 1", "File 2"],
folders: [
{
id:3,
files: ["File 1"],
folderName: "Subfolder of folder1",
folders: []
}
]
},
{
id: 2,
folderName: "Folder2",
files: ["File 1", "File 2"],
folders: []
}
]
};
然后创建一个组件来显示任何文件夹对象:
*.html
<h1>{{ folder.folderName }}</h1>
<ul>
<li *ngFor="let fileName of folder.files">
{{ fildeName }}
</li>
</ul>
<!-- Display subfolders -->
<display-folder *ngFor="let subFolder of folder.folders" [folder]="subFolder"></display-folder>
*.ts
@Component({
selector: 'display-folder',
...
})
export class DisplayFolder {
@Input() folder: Folder;
}
TA贡献1824条经验 获得超5个赞
我喜欢用ng-template这些东西。您可以定义一个可重复使用的模板,该模板反过来会呈现其自身。
<ng-container *ngFor="let folder of folders">
<ul>
<ng-container [ngTemplateOutlet]="folderNode" [ngTemplateOutletContext]="{$implicit:folder}"></ng-container>
</ul>
</ng-container>
<ng-template #folderNode let-folder>
<li> {{folder.folderName}} </li>
<li *ngFor="let file of folder.files">{{file}}</li>
<ng-container *ngIf="folder.folder">
<ng-container *ngFor="let subFolder of folder.folder">
<ul>
<ng-container [ngTemplateOutlet]="folderNode" [ngTemplateOutletContext]="{$implicit:subFolder}">
</ng-container>
</ul>
</ng-container>
</ng-container>
</ng-template>
TA贡献1806条经验 获得超5个赞
缩进的一种解决方案:
家长:
<ng-container *ngFor="let folder of folders">
<app-folder [folder]="folder" [indentationLevel]="0"></app-folder>
</ng-container>
孩子:
<div class="folder-row" style="margin-left: {{indentationLevel * 32}}px" (click)="folder.isExpanded = !folder.isExpanded">
<ion-icon class="folder-row__icon" *ngIf="folder.type === folderType.group" [name]="folder.isExpanded ? 'folder-open' : 'folder'"></ion-icon>
<img class="folder-row__user-image" src="assets/mock-data/{{folder.userId}}.jpg" *ngIf="folder.type === folderType.user" alt="👤">
{{folder.title}}
</div>
<ng-container *ngIf="folder.isExpanded">
<ng-container *ngFor="let folder of folder.subFolders">
<app-folder [folder]="folder" [indentationLevel]="indentationLevel + 1"></app-folder>
</ng-container>
</ng-container>
怎么运行的:
style="margin-left: {{indentationLevel * 32}}px"
这会根据类值创建内联样式
父级:基本情况:
[indentationLevel]="0"
文件夹: 进度:
[indentationLevel]="indentationLevel + 1"
- 3 回答
- 0 关注
- 111 浏览
添加回答
举报