VIV*_*VEK 7 html javascript lazy-loading typescript angular
我正在使用Angular创建TreeView,并且创建Treeview的场景是每个级别都有不同的API,这意味着如果我单击父级别节点之一,则仅子节点应该只为该特定节点生成,依此类推,对于进一步的级别,每个子节点列表都来自API
现在的问题是,当我在任何节点上单击时,正在为创建的树视图创建嵌套列表时。子节点正在为该节点以及其他节点生成。
这是我的代码。
<ul>
<li *ngFor="let item of companyList" [id]="item.id">
<span (click)="getLocation(item)">{{item.description}}</span>
<ul>
<li *ngFor="let loc of loactionData" [id]="loc.id">
<span (click)="getDepartment(loc)">{{loc.description}}</span>
<ul>
<li *ngFor="let depart of deaprtmentData">
<span (click)="getEmpStatus(depart)">{{depart.description}}</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
注意:每个列表都来自单独的API,这些click事件有助于调用API。
请帮助我解决上述问题,谢谢。
您正在为每个nesting重复相同的嵌套列表ul
。您必须将嵌套列表与他们的父母相关联。
由于您的companyList项目和locationData项目具有ID,因此请使用此ID将嵌套列表与每个公司和每个位置相关联。
要进行关联,请使用简单的对象在代码中创建一个地图,然后使用索引签名键入它。在您的情况下,它将如下所示:
companyList: Company[] = [];
locationData: { [companyId: string]: Location[] } = {};
departmentData: { [locationId: string]: Department[] } = {};
Run Code Online (Sandbox Code Playgroud)
然后,在模板中,您需要索引locationData
并departmentData
使用item.id
和loc.id
:
<ul>
<li *ngFor="let item of companyList" [id]="item.id">
<span (click)="getLocation(item)">{{ item.description }}</span>
<ul>
<li *ngFor="let loc of locationData[item.id]" [id]="loc.id">
<span (click)="getDepartment(loc)">{{ loc.description }}</span>
<ul>
<li *ngFor="let depart of departmentData[loc.id]">
<span (click)="getEmpStatus(depart)">{{ depart.description }}</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
生成数据时,请将其放在对象中正确的ID下:
getLocation(item: Company): void {
this.http.get<Location[]>(`${this.api}/locations?companyId=${item.id}`).subscribe(locations => {
this.locationData[item.id] = locations;
})
}
getDepartment(location: Location): void {
this.http.get<Department[]>(`${this.api}/departments?locationId=${location.id}`).subscribe(departments => {
this.departmentData[location.id] = departments;
})
}
Run Code Online (Sandbox Code Playgroud)
这是一个带有用户/帖子/评论数据模型和
jsonplaceholder
API 的示例。观看此Stackblitz演示以获取实时示例
<ul>
<li *ngFor="let user of users" [id]="user.id">
<span (click)="getPosts(user)">{{ user.name }}</span>
<ul>
<li *ngFor="let post of posts[user.id]" [id]="post.id">
<span (click)="getComments(post)">{{ post.title }}</span>
<ul>
<li *ngFor="let comment of comments[post.id]">
<span>{{ comment.name }}</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
interface User {
id: number;
name: string;
username: string;
email: string;
}
interface Post {
userId: number;
id: number;
title: string;
body: string;
}
interface Comment {
postId: number;
id: number;
name: string;
email: string;
body: string;
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
users: User[] = [];
posts: { [id: number]: Post[] } = {};
comments: { [id: number]: Comment[] } = {};
private api = 'https://jsonplaceholder.typicode.com';
constructor(private http: HttpClient) { }
ngOnInit(): void {
this.http.get<User[]>(`${this.api}/users`).subscribe(users => this.users = users);
}
getPosts(user: User): void {
this.http.get<Post[]>(`${this.api}/posts?userId=${user.id}`).subscribe(posts => {
this.posts[user.id] = posts;
})
}
getComments(post: Post): void {
this.http.get<Comment[]>(`${this.api}/comments?postId=${post.id}`).subscribe(comments => {
this.comments[post.id] = comments;
})
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1090 次 |
最近记录: |