我有自己的数据对象,我使用ngFor循环。但是对象中有一个数组部分,我想在<li></li>
目前它看起来像这样。但是,我不想在一个<li></li>
中使用完整的字符串,我只想要一个数组。
相关守则:
tier.model
export class Tier {
constructor (
public id: number,
public title: string,
public rewards: any[],
public price: number
)
{}
}
组件:
export class PackagesComponent { //will be renamed to TierComponent
public tierToEdit: Tier;
editMode = false;
tiers: Tier[] = [
new Tier(
10,
'First Tier',
['Server access - Simple', 'Win something small', 'and so on'],
5.00),
new Tier(
11,
'Second Tier',
['Server access - Standard', 'Win something medium', 'and so on and on'],
10.00),
new Tier(
12,
'Third Tier',
['Server access - Advanced', 'Win something big', 'and so on and on and on'],
10.00)
]
模板:
<div class="row">
<div class="col-md-4" *ngFor="let tier of tiers; let i=index;">
<ul class="pricing-table">
<li class="plan-name">
{{ tier.title }}
</li>
<li *ngFor="let reward of tiers">
{{ reward.rewards }}
</li>
<p class="plan-price">{{ tier.price }}</p>
<li class="plan-action">
<a class="btn btn-primary" (click)="onEdit(tier)">Edit</a>
<a class="btn btn-success" (click)="this.clickSave()">SaveYes</a>
<a class="btn btn-warning" >Get</a>
</li>
</ul>
最后它应该是这样的:
我试过的其他事情是这样的:
<div class="col-md-4" *ngFor="let tier of tiers; let i=index;">
<!-- Show Mode -->
<ul *ngIf="!editMode || tierToEdit != tier" class="pricing-table">
<li class="plan-name">
{{ tier.title }}
</li>
<li *ngFor="let reward of tiers; let i=index">
{{ reward.rewards[i] }}
</li>
比给我第一个数组的第一个元素,第二个数组的第二个元素和第三个数组的第三个元素:
希望这不是令人困惑,对于角度/ javascript仍然很新,抱歉可能会提出愚蠢的问题或使用错误的条款:)非常感谢你!
答案 0 :(得分:1)
您还需要在第二个*ngFor
中迭代嵌套数组,例如:
<div class="row">
<div class="col-md-4" *ngFor="let tier of tiers; let i=index;">
<ul class="pricing-table">
<li class="plan-name">
{{ tier.title }}
</li>
<li *ngFor="let reward of tier.rewards">
{{reward}}
</li>
<p class="plan-price">{{ tier.price }}</p>
<li class="plan-action">
<a class="btn btn-primary" (click)="onEdit(tier)">Edit</a>
<a class="btn btn-success" (click)="this.clickSave()">SaveYes</a>
<a class="btn btn-warning" >Get</a>
</li>
</ul>
</div>
</div>
答案 1 :(得分:0)
试试这个
<li *ngFor="let reward of tier.rewards">
{{ reward }}
</li>