angular - 设置包含所有相关项目的标题,
问题描述
我想显示一个有很多科目的学生列表,每个科目都有很多账单。我想在每个主题名称中设置标题,以显示与其相关的所有账单。现在我很难通过账单来区分主题,比如他们将再次调用主题的每张账单。
<h3>List of Student</h3>
<div *ngFor="let key of students | async; index as studentId">
<h2>
<a>
{{key.studentId}}
</a>.
{{key.studentName}}
</h2>
<div *ngFor="let elements of key.subjectSS">
<div *ngFor="let billDetail of elements.billSS">
<b>Subject:</b><span [title]="'Học phí: ' + billDetail.price"> {{elements.subject.subjectName}}</span><br />
<span>
<b>Bill:</b> {{billDetail.price}} VNĐ<br />
</span>
</div>
</div>
</div>
我的屏幕显示我的 Json
[ { "subjectSS": [ { "subject": { "studentSS": [], "subjectId": 1, "subjectName": "Math" }, "billSS": [ { "billId": 1, "price": 500, "reqStuSubId": 1 }, { "billId": 2, "price": 700, "reqStuSubId": 1 }, { "billId": 7, "price": 2400, "reqStuSubId": 1 } ], "reqStuSubId": 1, "studentId": 1, "subjectId": 1 }, { "subject": { "studentSS": [], "subjectId": 2, "subjectName": "Geography" }, "billSS": [ { "billId": 6, "price": 1000, "reqStuSubId": 2 }, { "billId": 9, "price": 424, "reqStuSubId": 2 } ], "reqStuSubId": 2, "studentId": 1, "subjectId": 2 } ], "studentId": 1, "studentName": "Hung" }, { "subjectSS": [ { "subject": { "studentSS": [], "subjectId": 3, "subjectName": "Physical" }, "billSS": [ { "billId": 5, "price": 900, "reqStuSubId": 7 }, { "billId": 8, "price": 745, "reqStuSubId": 7 } ], "reqStuSubId": 7, "studentId": 2, "subjectId": 3 } ], "studentId": 2, "studentName": "Manh" }, { "subjectSS": [], "studentId": 3, "studentName": "Long" } ]
解决方案
您必须更改 ts 文件中的 json,因为您不想在 html 中为相同的内容使用两个循环。
所以这是我的建议:
您的组件文件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
data: any = [
{
"subjectSS": [
{
"subject": {
"studentSS": [],
"subjectId": 1,
"subjectName": "Math"
},
"billSS": [
{
"billId": 1,
"price": 500,
"reqStuSubId": 1
},
{
"billId": 2,
"price": 700,
"reqStuSubId": 1
},
{
"billId": 7,
"price": 2400,
"reqStuSubId": 1
}
],
"reqStuSubId": 1,
"studentId": 1,
"subjectId": 1
},
{
"subject": {
"studentSS": [],
"subjectId": 2,
"subjectName": "Geography"
},
"billSS": [
{
"billId": 6,
"price": 1000,
"reqStuSubId": 2
},
{
"billId": 9,
"price": 424,
"reqStuSubId": 2
}
],
"reqStuSubId": 2,
"studentId": 1,
"subjectId": 2
}
],
"studentId": 1,
"studentName": "Hung"
},
{
"subjectSS": [
{
"subject": {
"studentSS": [],
"subjectId": 3,
"subjectName": "Physical"
},
"billSS": [
{
"billId": 5,
"price": 900,
"reqStuSubId": 7
},
{
"billId": 8,
"price": 745,
"reqStuSubId": 7
}
],
"reqStuSubId": 7,
"studentId": 2,
"subjectId": 3
}
],
"studentId": 2,
"studentName": "Manh"
},
{
"subjectSS": [],
"studentId": 3,
"studentName": "Long"
}
];
constructor() {
}
ngOnInit() {
this.data.map(x => {
x.subjectSS.map(y => {
let str = '';
for (let i = 0; i < y.billSS.length; i++) {
str += ((i === 0) ? '' : ',') + y.billSS[i].price;
}
y.subjectPrices = str;
})
})
console.log(this.data);
}
}
您的 HTML 文件
<h3>List of Student</h3>
<div *ngFor="let key of data; index as studentId">
<h2>
<a>
{{key.studentId}}
</a>.
{{key.studentName}}
</h2>
<div *ngFor="let elements of key.subjectSS">
<b>Subject:</b><span [title]="'Học phí: ' + elements.subjectPrices">
{{elements.subject.subjectName}}</span><br />
<span>
<div *ngFor="let billDetail of elements.billSS">
<b>Bill:</b> {{billDetail.price}} VNĐ<br /></div>
</span>
</div>
</div>
这是输出
推荐阅读
- c++ - 为什么具有无用的孤立“静态”的函数被认为是不纯的?
- javascript - 如何在谷歌地图中突出显示用户的当前位置?(就像新的 google.com/maps PWA)
- laravel - Laravel - 将 Stream 与新的 Http 客户端一起使用
- python - Django 递归注解
- javascript - 根据给定条件创建嵌套 json 对象
- kotlin - 设置器不修改二维数组
- r - 如何在大矩阵中组合具有相似名称的列
- python - Django:当我尝试从管理仪表板添加模型的新实例时返回 __str__ 错误
- python - 如何获取两个数据框列之间的交集项?
- javascript - 在缩放级别自动切换底图?