angular - 渲染列表项时显示无穷大的点
问题描述
我正在尝试渲染嵌套列表元素,
下面是JSON结构,
listItems = {
"text": "root",
"children": [{
"text": "Level 1",
"children": [{
"text": "Level 2",
"children": [{
"text": "Level 3",
"children": [],
"type": "unordered-list-item"
}],
"type": "unordered-list-item"
}],
"type": "unordered-list-item"
}],
"type": "unordered-list-item"
}
下面是我的组件 html,
<ul *ngIf="listItems.type === 'unordered-list-item' && listItems.children.length > 0">
<li *ngFor="let child of listItems.children">
{{ child.text }}
<bw-list *ngIf="child.type === 'unordered-list-item'" [listItem]="child"></bw-list>
</li>
</ul>
如果children
发现它需要重复组件并渲染子组件。
组件.ts
export class AppComponent {
@Input() listItem: any;
public listItems = {
"text": "root",
"children": [{
"text": "Level 1",
"children": [{
"text": "Level 2",
"children": [{
"text": "Level 3",
"children": [],
"type": "unordered-list-item"
}],
"type": "unordered-list-item"
}],
"type": "unordered-list-item"
}],
"type": "unordered-list-item"
};
}
问题是它呈现无穷大的点。请帮忙
链接到项目https://stackblitz.com/edit/angular-list-component-1smik4?file=src/app/app.component.ts
解决方案
我没有得到你的代码。你忘了减少长度数?也许这可以帮助你:
import {Component, Input} from "@angular/core";
@Component({
selector: 'my-app',
template: `
<div>
Recursive Component
<my-app [counter]="counter - 1" *ngIf="counter > 0"></my-app>
</div>
`
})
export class AppComponent {
@Input() counter : number = 3;
}
推荐阅读
- node.js - 如何在 graphql 中订阅动态主题?
- javascript - 如何在这个特定结构中创建 JSON 数组
- python - PermissionError: [Errno 13] Permission denied to access selenium python through webdriver-manager
- amazon-s3 - 如何使用 postdata preSigned Url 调用 Amazon S3 存储桶以使用空手道上传文件
- microsoft-cognitive - 训练微软 LUIS 区分不想要和想要
- php - 为什么我无法在免费网站上使用 php 代码连接到 msaccess 数据库 (.accdb)
- css - 如何在 vscode 中配置 stylelint 扩展?
- java - 如何将间隔类型格式化为 HH:MM 格式?
- android - 根据风味文件夹内容有条件地应用 gradle 插件
- java - Java Desktop.open() 永远不会完成